#include <iostream>
using namespace std;
int gcd(int x,int y){
   if(y==0)return x;
   return gcd(y,x%y);
}
int main(void){
    int a,b;
    cin>>a>>b;
    int k=gcd(a,b);
    string s,t;
    cin>>s>>t;
    for(int i=0;i<k;i++){
        if(s[(a/k)*i]!=t[(b/k)*i]){
            cout<<"-1"<<endl;
            return 0;
        }
    }
    cout<<(long long)a*b/k<<endl;
}
