#include<iostream>
#include<string>
using namespace std;
int main(){
  long long N,M,a,b,c,L;
  string S,T;
  cin >> N >> M >> S >> T;
  a=N;b=M;
  while(N != M){
    if(M>N) M=M-N;
    if(N>M) N=N-M;
  }
  c=M;
  L=a*b/c;
	for(int i=0;i<a;i++)
		if(i*b%a == 0&&S[i]!=T[i*b/a]){
				cout<<-1;
				return 0;
			}
	cout << L << endl;;
}