#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m, l;
  long long f1 = 0, f2 = 0;
  string s, t;
  cin >> n >> m >> s >> t;
  long long sign = 1;
  long long a, b, q;
  a = max(n, m);
  b = min(n, m);
  q = a % b;
  while (q != 0) {
    a = b;
    b = q;
    q = a % b;
  }
  long long n1 = n, m1 = m;
  n1 /= b;
  m1 /= b;
  int l1 = 0, l2 = 0;
  while (l1 < n && l2 < m) {
    f1 = l1 * m + 1;
    f2 = l2 * n + 1;
    if (f1 < f2)
      l1++;
    else if (f1 > f2)
      l2++;
    else {
      if (s[l1] != t[l2]) {
        sign = 0;
        break;
      }
      l1++;
      l2++;
    }
  }
  if (sign == 1)
    cout << n1 * m1 * b;
  else
    cout << -1;
  return 0;
}
