#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define pb push_back
#define mp make_pair
#define rep(i,n) for(int i = 0; i < (n); i++)
#define repr(i,b,e) for(int i = (b); i <= (e); i++)

#define pr(x) do{cerr << (#x) << " = " << (x) << endl;}while(0)
#define pri(x,i) do{cerr << (#x) << "[" << i << "] = " << (x[i]) << endl;}while(0)

namespace std {
	template<class S, class T> pair<S, T> operator + (const pair<S, T> &a, const pair<S, T> &b) {return mp<S, T>(a.first + b.first, a.second + b.second);}
	template<class S, class T> pair<S, T> operator - (const pair<S, T> &a, const pair<S, T> &b) {return mp<S, T>(a.first - b.first, a.second - b.second);}
	template<class S, class T> pair<S, T>& operator += (pair<S, T> &a, const pair<S, T> &other) {return a = a + other;}
	template<class S, class T> pair<S, T>& operator -= (pair<S, T> &a, const pair<S, T> &other) {return a = a - other;}
	template<class S, class T> ostream& operator << (ostream& ost, pair<S, T> p) {ost << "(" << p.first << ", " << p.second << ")";return ost;}
	template<class T> ostream& print_iterable_container(ostream& ost, T& container) {
		for (auto&& iter = begin(container); iter != end(container); ++iter) ost << (iter == begin(container) ? " [ " : ", ") << (*iter);
		return ost << " ]";
	}
#	define APPLY_PRINT_ITERABLE_CONTAINER(container_type)\
	template<class T> ostream& operator << (ostream& ost, container_type<T>& container) {\
		return print_iterable_container(ost, container);\
	}
	APPLY_PRINT_ITERABLE_CONTAINER(vector);
	APPLY_PRINT_ITERABLE_CONTAINER(list);
	APPLY_PRINT_ITERABLE_CONTAINER(deque);
}

template<class T> inline bool CHMAX(T &l,const T &r){return l<r?l=r,1:0;}
template<class T> inline bool CHMIN(T &l,const T &r){return l>r?l=r,1:0;}

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;
using vpii = vector<pii>;
using vpll = vector<pll>;

using ld = long double;

template<class T> using VV = vector<vector<T> >;
template<class T> using maxPQ = priority_queue<T>;
template<class T> using minPQ = priority_queue<T, vector<T>, greater<T> >;
template<class T, class U> using uomap = unordered_map<T, U>;
template<class T> using uoset = unordered_set<T>;

const ll INF = 1001001001;
const ll INFll = (ll)INF * INF;
const ll MOD = 1000000007;
const ll MOD_FFT = 998244353;
const ld EPS = 1e-15;

//function<void(int,int)> tensi_no_3p=[&](int v,int d){
//};

namespace number {
	// Return GCD of given two Ints. Always returns non-negative value.
	ll gcd(ll a, ll b) {
		if (b == 0) return a > 0 ? a : -a;
		else return gcd(b, a % b);
	}

	// Extended Euclid's algorithm.
	// Given two Ints (a, b), return two Ints (x, y) s.t. `ax + by = g`, where `g` is GCD of x and y.
	pll ext_gcd(ll a, ll b) {
		if (b == 0) {
			if (a == 0) return mp<ll>(0, 0);
			else if (a > 0) return mp<ll>(1, 0);
			else return mp<ll>(-1, 0);
		}
		else {
			pll e = ext_gcd(b, a % b);
			return mp(e.second, e.first - a / b * e.second);
		}
	}

	// Given two Ints (a, b), return two Ints (quotient, remainer) of `a / b`,
	// where 0 <= remainder < abs(b).
	pll div(ll a, ll b) {
		ll q = a / b;
		if (b * q > a) {
			if (b > 0) q--;
			else q++;
		}
		ll r = a - b * q;
		return mp(q, r);
	}
}

int main() {
	ll N, M; cin >> N >> M;
	string S, T; cin >> S >> T;
	if (N == M) {
		cout << (S == T ? N : -1) << endl;
		return 0;
	}
	if (S[0] != T[0]) {
		cout << -1 << endl;
		return 0;
	}

	ll G = number::gcd(N, M);
	int p = 0, q = 0;
	while (p < N && q < M) {
		if (S[p] != T[q]) {
			cout << -1 << endl;
			return 0;
		}
		p += N / G;
		q += M / G;
	}
	ll lcd = N * M / G;
	cout << lcd << endl;

	return 0;
}


Measured memory_bytes: 512