#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (a < b) {
    int temp = a;
    a = b;
    b = temp;
  }
  int r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
int main() {
  int n, m;
  cin >> n >> m;
  string s, t;
  cin >> s >> t;
  int l, a;
  a = gcd(n, m);
  l = (n * m) / a;
  n = n / a;
  m = m / a;
  bool ans = 1;
  if (n % m == 0 || m % n == 0)
    ans = (s != t) ? 0 : 1;
  else {
    for (int i = 0; i < a; i++) {
      int c = n * i + 1;
      int d = m * i + 1;
      if (s.at(c) = !t.at(d)) ans = 0;
    }
  }
  if (ans == 1)
    cout << l << endl;
  else
    cout << "-1" << endl;
}
