Language: C++

/*
@Date    : 2018-10-13 21:25:00
@Author  : Adscn (1349957827@qq.com)
@Link    : https://www.cnblogs.com/LLCSBlog
*/
typedef int it;
#define int long long
#ifdef FASTER
#pragma GCC diagnostic error "-std=c++11"
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#endif
#include<bits/stdc++.h>
using namespace std;
#define IL inline
#define RG register
#define gi getint()
#define gc getchar()
#define File(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
IL int getint()
{
    RG int xi=0;
    RG char ch=gc;
    bool f=0;
    while(ch<'0'|ch>'9')ch=='-'?f=1:f,ch=gc;
    while(ch>='0'&ch<='9')xi=(xi<<1)+(xi<<3)+ch-48,ch=gc;
    return f?-xi:xi;
}
IL void pi(int k,char ch=0)
{
    if(k<0)k=-k,putchar('-');
    if(k>=10)pi(k/10,0);
    putchar(k%10+'0');
    if(ch)putchar(ch);
}
IL unsigned int LOG2(unsigned int x)
{
    unsigned int ret;
    __asm__ __volatile__ ("bsrl %1, %%eax":"=a"(ret):"m"(x));
    return ret;
}
inline int gcd(int a,int b){while(a)a^=b^=a^=b%=a;return b;}
inline int lcm(int a,int b){return 1ll*a*b/gcd(a,b);}
string a,b;
bool check()
{
	int aa=lcm(a.size(),b.size())/a.size(),bb=lcm(a.size(),b.size())/b.size();
	int lc=lcm(a.size(),b.size());
	int i=0,j=0;
	for(i=0;i<a.size();i++)
	{
		while(j*bb<i*aa&&j<b.size())j++;
		if(j>=b.size())break;
		if(j*bb==i*aa&&a[i]!=b[j])return false;
	}
	return true;
}
it main(void)
{
	int n=gi,m=gi;
	cin>>a>>b;
	pi(check()?lcm(a.size(),b.size()):-1,'\n');
    return 0;
}

Compiler output:
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:13: error: 'long' and 'short' specified together
    7 | #define int long long
      |             ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:13: error: 'long' and 'short' specified together
    7 | #define int long long
      |             ^~~~
a.cc:7:13: error: 'long' and 'short' specified together
    7 | #define int long long
      |             ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:13: error: 'long' and 'short' specified together
    7 | #define int long long
      |             ^~~~
a.cc:7:13: error: 'long' and 'short' specified together
    7 | #define int long long
      |             ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:41,
                 from a.cc:58:
/usr/include/c++/14/cstddef:89:21: error: redefinition of 'struct std::__byte_operand<long long int>'
   89 |   template<> struct __byte_operand<long long> { using __type = byte; };
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/cstddef:85:21: note: previous definition of 'struct std::__byte_operand<long long int>'
   85 |   template<> struct __byte_operand<int> { using __type = byte; };
      |                     ^~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/cstddef:90:21: error: redefinition of 'struct std::__byte_operand<long long unsigned int>'
   90 |   template<> struct __byte_operand<unsigned long long> { using __type = byte; };
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/cstddef:86:21: note: previous definition of 'struct std::__byte_operand<long long unsigned int>'
   86 |   template<> struct __byte_operand<unsigned int> { using __type = byte; };
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:13: error: 'long long long' is too long for GCC
    7 | #define int long long
      |             ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:13: error: 'long long long' is too long for GCC
    7 | #define int long long
      |             ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:13: error: 'long long long' is too long for GCC
    7 | #define int long long
      |             ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:13: error: 'long long long' is too long for GCC
    7 | #define int long long
      |             ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:13: error: 'long long long' is too long for GCC
    7 | #define int long long
      |             ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:13: error: 'long long long' is too long for GCC
    7 | #define int long long
      |             ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:13: error: 'long long long' is too long for GCC
    7 | #define int long long
      |             ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:13: error: 'long long long' is too long for GCC
    7 | #define int long long
      |             ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:13: error: 'long long long' is too long for GCC
    7 | #define int long long
      |             ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:13: error: 'long long long' is too long for GCC
    7 | #define int long long
      |             ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:13: error: 'long long long' is too long for GCC
    7 | #define int long long
      |             ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:13: error: 'long long long' is too long for GCC
    7 | #define int long long
      |             ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:13: error: 'long long long' is too long for GCC
    7 | #define int long long
      |             ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long long' is too long for GCC
    7 | #define int long long
      |                  ^~~~
a.cc:7:18: error: 'long long lon