readi p,"Input the prime p";
w:=0;
F:=FiniteField(p); for i in [2..p-1] do
a:=F!i;
S:={a}; for j in [2..p-1] do
Include(~S,a^j);
end for; if#S eq p-1 then
w:=i; break;
end if;
end for;
print "p equals",p;
print "w equals",w;
ifnew eq 0 then break; end if;
end for; ifnew eq 0 then break; end if;
end for;
ifnew eq 1 then
Include(~mats1,[y1,y2,y3,y4]);
end if;
end for;
end for; if#mats1 eq (Gcd(p-1,3)*(p^2+3*p+11)+1)/2 then break; end if;
end for; if#mats1 eq (Gcd(p-1,3)*(p^2+3*p+11)+1)/2 then break; end if;
end for;
//Case babb=wbaaa
mats2:={};
for y1 in [0..(p-1) div 2] do for y2 in [0..p-1] do for y3 in [0..p-1] do for y4 in [0..p-1] do
ifnew eq 0 then break; end if;
end for; ifnew eq 0 then break; end if;
end for;
ifnew eq 1 then
Include(~mats2,[y1,y2,y3,y4]);
end if;
end for;
end for; if#mats2 eq (Gcd(p-1,3)*(p^2+p+1)+5)/2 then break; end if;
end for; if#mats2 eq (Gcd(p-1,3)*(p^2+p+1)+5)/2 then break; end if;
end for;