#include<iostream>
#include<string>
using namespace std;

long gcd(long n,long m){
  if(n>m) return gcd(m,n);
  else if(n==0) return m;
  else return gcd(m-n*(m/n),n);
}

int main()
{
  long N,M,g,n,m;
  string S,T;
  bool fl=1;
  cin >> N >> M >> S >> T;
  g=gcd(N,M);
  for(long i=0;i<g;i++){
    if(S[i*(N/g)]!=T[i*(M/g)]){
      fl=0;
      break;
    }
  }
  if(fl) cout << N*M/g;
  else cout << -1;
}