program Zadacha; {$APPTYPE CONSOLE} uses SysUtils; var i,j,n,m,k:integer; A,B:array[1..20,1..100000] of char; c:char; f,fl:boolean; f1,f2:text; begin { TODO -oUser -cConsole Main : Insert code here } assign(f1,'repair.in'); assign(f2,'repair.out'); reset(f1); rewrite(f2); readln(f1,n,m); for i:=1 to n do begin for j:=1 to m do begin read(f1,A[i,j]); end; readln(f1); end; for i:=1 to n do begin for j:=1 to m do begin read(f1,B[i,j]); end; readln(f1); end; f:=true; k:=0; while f=true do begin i:=1; f:=false; fl:=true; while fl=true do begin j:=1; while fl=true do begin if A[i,j]<>B[i,j] then begin f:=true;; fl:=false; end; inc(j); if j>m then begin fl:=false; j:=m*2; end; end; if j=m*2 then fl:=true; inc(i); if i>n then fl:=false; end; if f=true then begin inc(k); for i:=1 to n do begin c:=B[i,m]; for j:=m downto 2 do B[i,j]:=B[i,j-1]; B[i,1]:=c; end; end; end; writeln(f2,k); close(f1); close(f2); end.