($n,$m)=glob<>; @a=glob<>; @b=glob<>; @a[$_%$n]==@b[$_%$m]&&print($_+1)+exit for 0...$n*$m; print-1