#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  long long t = 1;
  while (b) {
    t = b;
    b = a % b;
    a = t;
  }
  return t;
}
int main() {
  long long n, m;
  string s, t;
  while (scanf("%lld%lld", &n, &m) != EOF) {
    cin >> s >> t;
    long long L = n * m / gcd(n, m);
    if (n >= m)
      for (long long i = 1; i <= m; i++)
        if ((i - 1) * (L / m) % (L / n) == 0 &&
            s[(i - 1) * (L / m) / (L / n)] != t[i - 1]) {
          printf("-1\n");
          return 0;
        }
    if (n < m)
      for (long long i = 1; i <= n; i++)
        if ((i - 1) * (L / n) % (L / m) == 0 &&
            s[i - 1] != t[(i - 1) * (L / n) / (L / m)]) {
          printf("-1\n");
          return 0;
        }
    printf("%lld\n", L);
  }
  return 0;
}
