#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (a < b) gcd(b, a);
  int r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
int main() {
  int N, M;
  cin >> N >> M;
  string S, T;
  cin >> S >> T;
  long long int GCD = gcd(M, N);
  int n = N / GCD;
  int m = M / GCD;
  bool out = false;
  for (int i = 0; i < GCD; i++) {
    if (S[i * n] != T[i * m]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << M * (N / GCD) << endl;
  return 0;
}
