program E; var f:text; mas1,mas2:array[1..20,1..100000] of char; i,j,n,m,kil:longint; function InStr(i:integer):boolean; var j:longint; res:boolean; begin j:=1; res:=true; while (res)and(j<=m) do begin if mas1[i,j]<>mas2[i,j] then res:=false; j:=j+1; end; InStr:=res; end; {___________________________________} procedure MyShr; var i,j:longint; begin for j:=m downto 1 do mas2[1,j+1]:=mas2[1,j]; for j:=m downto 1 do mas2[n-1,j+1]:=mas2[n-1,j]; mas2[1,1]:=mas2[1,m+1]; mas2[n-1,1]:=mas2[n-1,m+1]; kil:=kil+1; end; {_____________________________________} function Mas1Mas2:boolean; begin if (InStr(1))and(InStr(n-1)) then Mas1Mas2:=true else Mas1Mas2:=false; end; {_____________________________________} begin assign(f,'repair.in'); reset(f); readln(f,n,m); for i:=1 to n do begin for j:=1 to m do read(f,mas1[i,j]); readln(f); end; for i:=1 to n do begin for j:=1 to m do read(f,mas2[i,j]); readln(f); end; close(f); assign(f,'repair.out'); kil:=0; while not Mas1Mas2 do MyShr; rewrite(f); write(f,kil); close(f); end.