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

int gcd(int m, int n){
  if(n==0) return m;
  else return gcd(n, m%n);
}

int main(){
  int n, m;
  cin >> n >> m;
  string s, t;
  cin >> s;
  cin >> t;
  int g = gcd(n, m);

  for(int i = 0; i < g; i++){
    if( s[i*n/g] != t[i*m/g] ){
      cout << "-1\n";
      return 0;
    }
  }
  cout << 1ll*n*m/g << "\n";
  return 0;
}
