#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll gcd(ll a,ll b){
  if(a<b) swap(a,b);
  if(a%b) return gcd(b,a%b);
  else return b;
}
int main(){
  ll a,b;
  string s,t,c,d;
  cin>>a>>b>>s>>t;
  ll g=gcd(a,b);
  for(int i=0;i<a;i+=a/g) c.push_back(s[i]);
  for(int j=0;j<b;j+=b/g) d.push_back(t[j]);
  cout<<(c==d?a/g*b:-1);
}