#include<stdio.h>
int main(void){
  char *s="python3 -c\""
"from fractions import gcd\n"
"n,m=map(int,input().split())\n"
"s=input()\n"
"t=input()\n"
"g=gcd(n,m)\n"
"a=n//g\n"
"b=m//g\n"
"for i in range(g):\n"
"  if s[a*i]!=t[b*i]:\n"
"    print(-1)\n"
"    break\n"
"else:\n"
"  print(n*m//g)\n"
"\"";
  system(s);
  return 0;
}            