Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
n,m = map(int,input().split())
s = str(input())
t = str(input())
a = 0
def get_gcd(a, b):
    if a < b:
        a, b = b, a
    if a % b == 0:
        return b
    else:
        return get_gcd(b, a % b)
def get_lcm(a, b, gcd):
    lcm = a * b // gcd
    return lcm
saisyou = get_lcm(n,m,get_gcd(n,m))
ans = 0
if saisyou == n or saisyou == m:
    print(-1)
else:
    for l in range(saisyou,100000,saisyou):
        num = 0
        xlis = []
        ylis = []
        for i in range(min(n,m)):
            xlis.append(1+(l/n)*i+1)
            ylis.append(1+(l/m)*i+1)
        yset = set(ylis)
        xset = set(xlis)
        renketu = set(yset + xset)
        if len(yset) + len(xset) == renketu:
            ans = l
            break
    print(ans)    

n,m = map(int,input().split())
s = str(input())
t = str(input())
a = 0
def get_gcd(a, b):
    if a < b:
        a, b = b, a
    if a % b == 0:
        return b
    else:
        return get_gcd(b, a % b)
def get_lcm(a, b, gcd):
    lcm = a * b // gcd
    return lcm
saisyou = get_lcm(n,m,get_gcd(n,m))
ans = 0
if saisyou == n or saisyou == m:
    print(-1)
else:
    for l in range(saisyou,100000,saisyou):
        num = 0
        xlis = []
        ylis = []
        for i in range(min(n,m)):
            xlis.append(1+(l/n)*i+1)
            ylis.append(1+(l/m)*i+1)
        yset = set(ylis)
        xset = set(xlis)
        for i in yset:
            hanntei = 0
            for j in xset:
                if i == j:
                    if s[j] == t[i]:
                        break
                    else:
                        hanntei += 1
                        break
            if hanntei >= 1:
                break
        num += 1
        ans = l
        break 
    if num == 1:
        print(ans)    
    else:
        print(-1)

n,m = map(int,input().split())
s = str(input())
t = str(input())
if n > m:
    bigg = n
    summoo = m
def get_gcd(a, b):
    if a < b:
        a, b = b, a
    if a % b == 0:
        return b
    else:
        return get_gcd(b, a % b)
def get_lcm(a, b, gcd):
    lcm = a * b // gcd
    return lcm
saisyou = get_lcm(n,m,get_gcd(n,m))
unti = max(saisyou,n,m)
unti = saisyou((unti//saisyou)*saisyou + saisyou)
ans = 0
if bigg%summoo != 0:
    for l in range(unti,100000,saisyou):
        num = 0
        for i in range(min(n,m)):
            if 1+(l/n)*i+1 == 1+(l/m)*i+1:
                if s[i] == t[i]:
                    pass
                else:
                    num += 1
                    break
        if num == 0:
            ans = l
            break
    print(ans)
else:
    print(-1)

n,m = map(int,input().split())
s = str(input())
t = str(input())
a = 0
def get_gcd(a, b):
    if a < b:
        a, b = b, a
    if a % b == 0:
        return b
    else:
        return get_gcd(b, a % b)
def get_lcm(a, b, gcd):
    lcm = a * b // gcd
    return lcm
saisyou = get_lcm(n,m,get_gcd(n,m))
ans = 0
if saisyou == n or saisyou == m:
    print(-1)
else:
    for l in range(saisyou,100000,saisyou):
        num = 0
        xlis = []
        ylis = []
        for i in range(min(n,m)):
            xlis.append(1+(l/n)*i+1)
            ylis.append(1+(l/m)*i+1)
        yset = set(ylis)
        xset = set(xlis)
        for i in yset:
            hanntei = 0
            for j in xset:
                if i == j:
                    if s[j-1] == t[i-1]:
                        break
                    else:
                        hanntei += 1
                        break
            if hanntei >= 1:
                break
        num += 1
        ans = l
        break 
    if num == 1:
        print(ans)    
    else:
        print(-1)

n,m = map(int,input().split())
s = str(input())
t = str(input())
def get_gcd(a, b): 
    if a < b:
        a, b = b, a
    if a % b == 0:
        return b
    else:
        return get_gcd(b, a % b)
def get_lcm(a, b, gcd): 
    lcm = a * b // gcd
    return lcm
lcm = get_lcm(n,m,get_gcd(n,m)) 
lcm2 = get_lcm(lcm/n,lcm/m,get_gcd(lcm/n,lcm/m)) 
ans = 0
for i in range(int(lcm//lcm2)):
    if s[int(i*lcm2//(lcm/n))] == t[int(i*lcm2//(lcm/m))]:
        pass
    else:
        ans = 1
        break
if ans == 1:
    print(-1)
else:
    print(lcm)

Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Accepted