using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Practice
{
    class Program
    {
        static void Main(string[] args)
        {
            SolveA();
            //SolveB();
            //SolveC();
            //SolveD();
        }
        private const long MOD = 1000000007;
        static void SolveA()
        {
            var nm = ReadAndParseIntArr();
            int n = nm[0];
            int m = nm[1];
            string s = Console.ReadLine();
            string t = Console.ReadLine();
            if(s[0] != t[0])
            {
                Console.WriteLine(-1);
                return;
            }
            if( n == m)
            {
                if( s == t)
                {
                    Console.WriteLine(n);
                }
                else
                {
                    Console.WriteLine(-1);
                }
                return;
            }

            long l = lcm(n, m);
            if( l <= 0)
            {
                Console.WriteLine(-1);
                return;
            }
            if(l < n || l < m)
            {
                Console.WriteLine(-1);
                return;
            }
            // 互いに素なら衝突しなさそう
            long ln = l / n;
            long lm = l / m;
            if( isPrime(ln) && isPrime(lm))
            {
                Console.WriteLine(l);
            }
            else
            {
                // 真面目に判定するか…
                var x = CreateDummyString(l, s);
                if (IsValid(l, t, x))
                {
                    Console.WriteLine(l);
                }
                else
                {
                    Console.WriteLine(-1);
                }
            }
        }
        private static Dictionary<long, char> CreateDummyString(long l, string s)
        {
            long n = s.Length;
            long coef = l / n;

            var x = new Dictionary<long, char>();
            for (long i =0;i<n;++i)
            {
                long idx = i * coef;
                x.Add(idx, s[(int)i]);
            }
            return x;
        }
        private static bool IsValid(long l, string t, Dictionary<long, char> x)
        {
            long m = t.Length;
            long coef = l / m;
            for(long i =0;i< m; ++i)
            {
                long idx =  i * coef;
                if( x.ContainsKey(idx) && x[idx] != t[(int)i]) { return false; }
            }
            return true;
        }
        private static bool isPrime(long x)
        {
            if( x == 2) { return true; }
            if( x < 2 || x % 2 == 0) { return false; }
            long i = 3;
            while( i * i <= x)
            {
                if( x % i == 0) { return false; }
                i = i + 2;
            }
            return true;
        }
        private static long lcm(long x, long y)
        {
            // todo オーバーフローしない？
            long g = gcd(x, y);
            if( g <= 0)
            {
                return -1;
            }
            return x  / g*y;
        }
        private static long gcd(long x, long y)
        {
            if(x < y)
            {
                long tmp = y;
                y = x;
                x = tmp;
            }
            while(y > 0)
            {
                long r = x % y;
                x = y;
                y = r;
            }
            return x;
        }

        private static int ReadAndParseInt()
        {
            return int.Parse(Console.ReadLine());
        }
        private static int[] ReadAndParseIntArr()
        {
            return Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
        }
        private static long ReadAndParseLong()
        {
            return long.Parse(Console.ReadLine());
        }
        private static long[] ReadAndParseLongArr()
        {
            return Array.ConvertAll(Console.ReadLine().Split(' '), long.Parse);
        }
    }
}

Measured memory_bytes: 19808