program Project2; {$APPTYPE CONSOLE} var f:text; p1,p2, p0:boolean; s,d:array[1..100000,1..2] of ansistring; a,b,n,i, n1,n2:longint; procedure zmin(f,d:longint); var k:array[1..1000,1..2] of ansistring; i:longint; begin k[f,d][1]:=s[f,d][b]; for i:=2 to b do k[f,d][i]:=s[f,d][i-1]; s[f,d]:=k[f,d] end; begin assign(f, 'repair.in'); reset(f); readln(f, a, b); for i:=1 to a do begin readln(f, s[i, 1]); s[i, 1]:=d[i,1]; end; for i:=1 to a do begin readln(f, s[i, 2]); s[i, 2]:=d[i,2]; end; close(f); n1:=0; n2:=0; p0:=true; for i:=1 to a do if s[i,1]<>s[i,2] then p0:=False; while not p0 do begin for i:=1 to a do begin zmin(i,1); zmin(i,2); end; p1:=true; p2:=true; for i:= 1 to a do begin if d[i,1]<>s[i,2] then p1:=false; if d[i,2]<>s[i,1] then p2:=false; end; if not p1 then inc(n1); if not p2 then inc(n2); if p1 or p2 then p0:=True; end; assign(f, 'repair.out'); rewrite(f); if p1 then writeln(f, n1) else if p2 then writeln(f, n2) else writeln(f, '0'); close(f); end.