procedure TForm1.Button1Click(Sender: TObject);
var
un,sn:string;
i,j,reg:integer;
tb:array[1..9] of byte;
begin
if (length(edit1.Text)>0) and (length(edit2.Text)>=9) then
try
begin
un:=edit1.Text;
sn:=edit2.Text;
reg:=0;
while length(un)<9 do
begin
un:=un+un;
end;
for i:=1 to 9 do
begin
tb[i]:=abs(ord(un[i])-ord(sn[i]));
for j:=2 to i-1 do
begin
if tb[j]=tb[i] then exit;
end;
end;
if tb[3]+tb[5]+tb[7]<>15 then reg:=reg+1;
un:=chr(75);
for i:=1 to 3 do
begin
if tb[3*(i-1)+1]+tb[3*(i-1)+2]+tb[3*(i-1)+3]<>15 then reg:=reg+1;
if tb[i]+tb[i+3]+tb[i+6]<>15 then reg:=reg+1;
end;
un:=un+chr(33);
if tb[1]+tb[5]+tb[9]<>15 then reg:=reg+1;
un:=chr(79)+un;
i:=j div reg;
sn:=inttostr(i+j);
end;
except
begin
showmessage(un);
end;
end;
end;