Problem:


Score : 300 points

Problem Statement

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.

Constraints

1 \leq N,M \leq 10^5

S and T consist of lowercase English letters.

|S|=N

|T|=M

Input

Input is given from Standard Input in the following format:

N M
S
T

Output

If a good string does not exist, print -1; if it exists, print the length of the shortest such string.

Sample Input 1

3 2
acp
ae

Sample Output 1

6

For example, the string accept is a good string.
There is no good string shorter than this, so the answer is 6.

Sample Input 2

6 3
abcdef
abc

Sample Output 2

-1

Sample Input 3

15 9
dnsusrayukuaiia
dujrunuma

Sample Output 3

45



Sample input:
3 2
acp
ae


Sample output:
6

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
s, t = list(input()), list(input())


def gcd(a, b):
    if a > b:
        return gcd(b, a)
    while a > 0:
        a, b = b % a, a
    return b


def lcm(a, b):
    return a * b // gcd(a, b)


l = lcm(len(s), len(t))
x = ["無"] * l
for i in range(0, len(s)):
    x[i * (l // len(s))] = s[i]
for i in range(0, len(t)):
    if x[i * (l // len(t))] == "無":
        continue
    else:
        if x[i * (l // len(t))] != t[i]:
            print(-1)
            exit()
print(l)

Submission status: Runtime Error

Solution code:
from math import gcd
n, m = map(int, input().split())
s = list(input())
t = list(input())

ans = n*m//gcd(n, m)
sp_a, sp_b = ans//n, ans//m
l = [False]*(ans + 1)
jud = True
for i in range(n):
    l[i*sp_a] = s[i]

for i in range(m):
    if not l[i*sp_b]:
        l[i*sp_b] = t[i]
    else:
        if l[i*sp_b] != t[i]:
            jud = False
            break
if jud:
    print(ans)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
from math import gcd
n, m = map(int, input().split())
s = list(input())
t = list(input())

ans = n*m//gcd(n, m)
sp_a, sp_b = ans//n, ans//m
l = [False]*ans
jud = True
for i in range(n):
    l[i*sp_a] = s[i]

for i in range(m):
    if not l[i*sp_b]:
        l[i*sp_b] = t[i]
    else:
        if l[i*sp_b] != t[i]:
            jud = False
            break
if jud:
    print(ans)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
s = list(input())
t = list(input())

def gcd(a, b):
    while b != 0:
        c = a%b
        a = b
        b = c
    return a

ans = n*m//gcd(n, m)
sp_a, sp_b = ans//n, ans//m
l = [False]*ans
jud = True
for i in range(n):
    l[i*sp_a] = s[i]

for i in range(m):
    if not l[i*sp_b]:
        l[i*sp_b] = t[i]
    else:
        if l[i*sp_b] != t[i]:
            jud = False
            break
if jud:
    print(ans)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import math
def lcm(x, y):
    return (x * y) // math.gcd(x, y)
 
N,M = map(int,input().split())
S = list(str(input()))
T = list(str(input()))
MOD = 10**9+7
L = lcm(N,M)
if L == max(N,M):
  L *= 2
X = [""]*(L%MOD)
if S[0] != T[0]:
  print(-1)
  exit()
else:
  X[0] = S[0]
 
for i in range(1,N):
  X[i*L//N] = S[i]
for j in range(1,M):
  if X[j*L//M] == "" or X[j*L//M] == T[j]:
    pass
  else:
    print(-1)
    exit()
 
print(L)

Submission status: Runtime Error

Solution code:
import math
def lcm(x, y):
    return (x * y) // math.gcd(x, y)

N,M = map(int,input().split())
S = list(str(input()))
T = list(str(input()))

L = lcm(N,M)
if L == max(N,M):
  L *= 2
X = [""]*L
if S[0] != T[0]:
  print(-1)
  exit()
else:
  X[0] = S[0]

for i in range(1,N):
  X[i*L//N] = S[i]
for j in range(1,M):
  if X[j*L//M] == "" or X[j*L//M] == T[j]:
    pass
  else:
    print(-1)
    exit()

print(L)

Submission status: Runtime Error

Solution code:
import math
def lcm(x, y):
    return (x * y) // math.gcd(x, y)

N,M = map(int,input().split())
S = str(input())
T = str(input())

L = lcm(N,M)
if L == max(N,M):
  L *= 2
X = [""]*(L+10**5)
if S[0] != T[0]:
  print(-1)
  exit()
else:
  X[0] = S[0]

for i in range(1,N):
  X[i*L//N] = S[i]
for j in range(1,M):
  if X[j*L//M] == "" or X[j*L//M] == T[j]:
    pass
  else:
    print(-1)
    exit()

print(L)

Submission status: Runtime Error

Solution code:
import math
def lcm(x, y):
    return (x * y) // math.gcd(x, y)

N,M = map(int,input().split())
S = str(input())
T = str(input())

L = lcm(N,M)
if L == max(N,M):
  L *= 2
X = [""]*L
if S[0] != T[0]:
  print(-1)
  exit()
else:
  X[0] = S[0]

for i in range(1,N):
  X[i*L//N] = S[i]
for j in range(1,M):
  if X[j*L//M] == "" or X[j*L//M] == T[j]:
    pass
  else:
    print(-1)
    exit()

print(L)

Submission status: Runtime Error

Solution code:
import math
def lcm(x, y):
    return (x * y) // math.gcd(x, y)

N,M = map(int,input().split())
S = str(input())
T = str(input())

L = lcm(N,M)
if L == max(N,M):
  L *= 2
X = [""]*L
if S[0] != T[0]:
  print(-1)
  exit()
else:
  X[0] = S[0]

for i in range(1,N):
  X[i*L//N] = S[i]
for j in range(1,M):
  if X[j*L//M] == "":
    X[j*L//M] = T[j]
  elif X[j*L//M] == T[j]:
    pass
  else:
    print(-1)
    exit()

print(L)

Submission status: Runtime Error

Solution code:
import math
N, M = map(int, input().split())
S = list(input())
T = list(input())

lcm = (N * M) // math.gcd(N, M)
ans = lcm
ansS = ["0"]*lcm
ansT = ["0"]*lcm
Sp = lcm//N
Tp = lcm//M

for i in range(Sp*lcm):
    if Sp*i > lcm:
        break
    if i > N-1:
        break
    ansS[Sp*i] = S[i]

for i in range(Tp*lcm):
    if Tp*i > lcm:
        break
    if i > M-1:
        break
    ansT[Tp*i] = T[i]

for i in range(lcm):
    if ansS[i] == "0" or ansT[i] == "0":
        continue
    if ansS[i] != ansT[i]:
        ans = -1
        break

print(ans)

Submission status: Runtime Error

Solution code:
import math
N, M = map(int, input().split())
S = list(input())
T = list(input())

lcm = (N * M) // math.gcd(N, M)
ans = lcm
ansS = ["0"]*lcm
ansT = ["0"]*lcm
Sp = lcm//N
Tp = lcm//M

for i in range(10000000000):
    if Sp*i > lcm:
        break
    if i > N-1:
        break
    ansS[Sp*i] = S[i]

for i in range(10000000000):
    if Tp*i > lcm:
        break
    if i > M-1:
        break
    ansT[Tp*i] = T[i]

for i in range(lcm):
    if ansS[i] == "0" or ansT[i] == "0":
        continue
    if ansS[i] != ansT[i]:
        ans = -1
        break

print(ans)

Submission status: Runtime Error

Solution code:
import math
N, M = map(int, input().split())
S = list(input())
T = list(input())

lcm = (N * M) // math.gcd(N, M)
ans = lcm
ansS = ["0"]*lcm
ansT = ["0"]*lcm
Sp = lcm//N
Tp = lcm//M


for i in range(10000000000):
    if Sp*i > lcm:
        break
    if i > N:
        break
    ansS[Sp*i] = S[i]

for i in range(10000000000):
    if Tp*i > lcm:
        break
    if i > M:
        break
    ansT[Tp*i] = T[i]

for i in range(lcm):
    if ansS[i] == "0" or ansT[i] == "0":
        continue
    if ansS[i] != ansT[i]:
        ans = -1
        break
print(ans)

Submission status: Runtime Error

Solution code:
import math
N, M = map(int, input().split())
S = list(input())
T = list(input())
lcm = (N * M) // math.gcd(N, M)
ans = lcm
ansS = ["0"]*lcm
ansT = ["0"]*lcm
Sp = lcm//N
Tp = lcm//M
i = 1
for i in range(10000000000):
    if Sp*i > lcm:
        break
    if i >= N:
        break
    ansS[Sp*i] = S[i]

for i in range(10000000000):
    if Tp*i > lcm:
        break
    if i >= M:
        break
    ansT[Tp*i] = T[i]

for i in range(lcm):
    if ansS[i] == "0" or ansT[i] == "0":
        continue
    if ansS[i] != ansT[i]:
        ans = -1
        break
print(ans)

Submission status: Runtime Error

Solution code:
import math

def lcm(a, b):
    return a * b // math.gcd(a, b)

N,M = map(int, input().split())
S = input()
T = input()

x = lcm(N, M)

ans = True
X = [''] * x
for i in range(N):
    j = i * x // N + 1
    if x > j:
        X[j] = S[i]

for i in range(M):
    j = i * x // M + 1
    if X[j]:
        if X[j] == T[i]:
            continue
        else:
            ans = False
            break
    if x > j:
        X[j] = T[i]

print(x if ans else -1)

Submission status: Runtime Error

Solution code:
from math import gcd
N, M = map(int, input().split())
S = input()
T = input()

L = N // gcd(N, M) * M
memo = [0] * (L + 10)


ok = True

ds = L // N
for i in range(N):
    memo[i * ds] = S[i]
dt = L // M
for i in range(M):
    if not memo[i * dt]:
        continue
    if memo[i * dt] != T[i]:
        ok = False

if ok:
    print(L)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
from math import gcd
N, M = map(int, input().split())
S = input()
T = input()

L = N // gcd(N, M) * M
memo = [0] * L


ok = True

ds = L // N
for i in range(N):
    memo[i * ds] = S[i]
dt = L // M
for i in range(M):
    if not memo[i * dt]:
        continue
    if memo[i * dt] != T[i]:
        ok = False

if ok:
    print(L)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import math
N,M = (int(T) for T in input().split())
S = input()
T = input()

Length = N*M//math.gcd(N,M)
String = ['']*Length
Flag   = True

for Nk in range(0,N):
    String[Nk*(Length//N)] = S[Nk]

for Mk in range(0,M):
    Target = String[Mk*(Length//M)]
    if Target=='' or Target==T[Mk]:
        String[Mk*(Length//M)] = T[Mk]
    else:
        Flag = False
        break        
if Flag:
    print(Length)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
S = str(input())
T = str(input())

if M > N:
	M, N = N, M
	S, T = T, S

import math
G = N*M//math.gcd(N, M)

A = []
B = []

for i in range (0, N):
	A.append(1+i*G//N) 
for i in range (0, M):
	B.append(1+i*G//M)

J = [0]*(G+1)

for i in range (0, N):
	J[A[i]]+= (i+1)

FA = []
FB = []

for i in range (0, M):
	if J[B[i]] != 0:
		FB.append(i)
		FA.append(J[B[i]]-1)
        
for i in range (0, len(FA)):
	if S[FA[i]] != T[FB[i]]:
		print(-1)
		exit()
print(G)

Submission status: Runtime Error

Solution code:
import math
N,M = (int(T) for T in input().split())
Length = N*M//math.gcd(N,M)
S = input()
T = input()
String = ['']*Length
Flag   = True
for Nk in range(0,N):
    String[Nk*(Length//N)] = S[Nk]
    
for Mk in range(0,M):
    Target = String[Mk*(Length//M)]
    if Target=='' or Target==T[Mk]:
        String[Mk*(Length//M)] = T[Mk]
    else:
        Flag = False
if Flag:
    print(Length)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
from math import gcd
N, M = map(int, input().split())
S = input()
T = input()

g = N // gcd(N, M) * M
res = [-1] * g
for i, s in enumerate(S):
    x = (g // N) * i
    res[x] = s

for i, s in enumerate(T):
    x = (g // M) * i
    if res[x] == -1:
        res[x] = s
    elif res[x] != s:
        print(-1)
        exit()

print(g)

Submission status: Runtime Error

Solution code:
import sys
from math import gcd
input = sys.stdin.readline
N, M = map(int, input().split())
S = list(input())[: -1]
T = list(input())[: -1]
ln = N * M // gcd(N, M)
res = ["a"] * ln
x = 0
y = 0
for i in range(ln):
  if i == x * ln // N and (i == y * ln // M):
    if S[x] != T[y]:
      print(-1)
      exit(0)
    res[i] = S[x]
    x += 1
    y += 1
  elif i == x * ln // N:
    res[i] = S[x]
    x += 1
  elif i == y * ln // M:
    res[i] = T[y]
    y += 1
print(len(res))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

# from numba import njit
from math import gcd
# input = stdin.readline

def lcm(x,y):
  return x*y//gcd(x,y)

# @njit
def solve(n,m,s,t):
  L = lcm(n,m)
  
  lst = [""]*L
  s_interval = L//n
  t_interval = L//m
  for i in range(n):
    lst[i*s_interval] = s[i]
  for i in range(m):
    if lst[i*t_interval] =="":
      continue
    if lst[i*t_interval] != t[i]:
      return "-1"
  return L
  

def main():
  N,M = map(int,input().split())
  s = input()
  t = input()
  print(solve(N,M,s,t))
  return

if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

# from numba import njit
from math import gcd
# input = stdin.readline

def lcm(x,y):
  return x*y//gcd(x,y)

# @njit
def solve(n,m,s,t):
  L = lcm(n,m)
  
  lst = [""]*L
  s_interval = L//n
  t_interval = L//m
  for i in range(n):
    lst[i*s_interval] = s[i]
  for i in range(m):
    if lst[i*t_interval] =="":
      continue
    if lst[i*t_interval] != t[i]:
      return -1
  return L
  

def main():
  N,M = map(int,input().split())
  s = input()
  t = input()
  print(solve(N,M,s,t))
  return

if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

# from numba import njit
from math import gcd
# input = stdin.readline

def lcm(x,y):
  return x*y//gcd(x,y)

# @njit
def solve(n,m,s,t):
  L = lcm(n,m)
  
  lst = [""]*L
  s_interval = L//n
  t_interval = L//m
  for i in range(n):
    lst[i*s_interval] = s[i]
  for i in range(m):
    if lst[i*t_interval] =="":
      continue
    if lst[i*t_interval] != t[i]:
      return -1
  return L
  
  
  return n



def main():
  N,M = map(int,input().split())
  s = input()
  t = input()
  print(solve(N,M,s,t))
  return

if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
import math
def main5():
    N, M = map(int, input().split())
    S = input()
    T = input()

    G = math.gcd(N, M)
    L = (N * M) // G

    for i in range(G):
        if S[i * N // G] != T[i * M // G]:
            print(-1)
            exit()

    print(L)

if __name__ == "__main__":
    main4()

Submission status: Runtime Error

Solution code:
import math
n,m=map(int,input().split())
s=list(input())
t=list(input())
num=(n*m)//math.gcd(n,m)
str1=["A"]*num
ans=num
numn=num//n
numm=num//m
for i in range(n):
    str1[i*(numn)]=s[i]
for i in range(m):
    str2=str1[i*numm]
    if str2!="A" and str2!=t[i]:
        ans=-1
        break
print(ans)

Submission status: Runtime Error

Solution code:
import math
n,m=map(int,input().split())
s=list(input())
t=list(input())
num=(n*m)//math.gcd(n,m)
str1=[0]*num
ans=num
numn=num//n
numm=num//m
for i in range(n):
    str1[i*(numn)]=s[i]
for i in range(m):
    str2=str1[i*numm]
    if str2!=0 and str2!=t[i]:
        ans=-1
        break
print(ans)

Submission status: Runtime Error

Solution code:
def gcd(a, b):
    if a < b:
        a, b = b, a
    while a % b != 0:
        a, b = b, a % b
    return b


N, M = map(int, input().split())
S = input()
T = input()
x = gcd(N, M)
n = N // x
m = M // x

for i in range(3):
    if S[n*i] == T[m*i]:
        print(-1)
        break
print(N*M//x)

Submission status: Runtime Error

Solution code:
import math
n,m = map(int,input().split())
s = input()
t = input()
lcm = n*m//math.gcd(n,m)
ans = [0]*lcm
for i in range(n):
    ans[i*(lcm)//n] = s[i]
for i in range(m):
    if ans[i*(lcm)//m] != t[i] and 0 != ans[i*(lcm)//m]:
        print(-1)
        exit()
print(lcm)

Submission status: Runtime Error

Solution code:
import math

n,m = map(int,input().split())
s = input()
t = input()
L = n * m // math.gcd(n, m)
ans = 1
u = [0]*(10**6)
for i,x in enumerate(s):
    if i == 0:
        u[0] = x
    else:
        u[L//n*i] = x
for i,x in enumerate(t):
    if i == 0:
        if u[0] == x:
            u[0] = x
        else:
            ans = 0
    else:
        if u[L//m*i] == 0 or u[L//m*i] == x:
            u[L//m*i] = x
        else:
            ans = 0
print([-1,L][ans])

Submission status: Runtime Error

Solution code:
import math

n,m = map(int,input().split())
s = input()
t = input()
L = n * m // math.gcd(n, m)
ans = 1
u = [0]*L
for i,x in enumerate(s):
    if i == 0:
        u[0] = x
    else:
        u[L//n*i-1] = x
for i,x in enumerate(t):
    if i == 0:
        if u[0] == x:
            u[0] = x
        else:
            ans = 0
            break
    else:
        if u[L//m*i-1] == 0 or u[L//m*i-1] == x:
            u[L//m*i-1] = x
        else:
            ans = 0
            break
print([-1,L][ans])

Submission status: Runtime Error

Solution code:
def lcm(a, b):
    if a<b:
        a,b = b,a
    x = a*b
    r = a%b
    while r != 0:
        a = b
        b = r
        r = a % b
    return x//b

n,m = map(int,input().split())
s = input()
t = input()
L = lcm(n,m)
ans = 1
u = [0]*L
for i,x in enumerate(s):
    if L//n*i-1 > n:
        ans = 0
        break
    if i == 0:
        u[0] = x
    else:
        u[L//n*i-1] = x
for i,x in enumerate(t):
    if L//m*i-1 > m:
        ans = 0
        break
    if i == 0:
        if u[0] == x:
            u[0] = x
        else:
            ans = 0
            break
    else:
        if u[L//m*i-1] == 0 or u[L//m*i-1] == x:
            u[L//m*i-1] = x
        else:
            ans = 0
            break
print([-1,L][ans])

Submission status: Runtime Error

Solution code:
def lcm(a, b):
    if a<b:
        a,b = b,a
    x = a*b
    r = a%b
    while r != 0:
        a = b
        b = r
        r = a % b
    return x//b

n,m = map(int,input().split())
s = input()
t = input()
L = lcm(n,m)
ans = 1
u = [0]*L
for i,x in enumerate(s):
    if i == 0:
        u[0] = x
    else:
        u[L//n*i-1] = x
for i,x in enumerate(t):
    if i == 0:
        if u[0] == x:
            u[0] = x
        else:
            ans = 0
            break
    else:
        if u[L//m*i-1] == 0 or u[L//m*i-1] == x:
            u[L//m*i-1] = x
        else:
            ans = 0
            break
print([-1,L][ans])

Submission status: Runtime Error

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,m = I()
s = v()
t = v()
l = lcm(n,m)
g = gcd(n,m)

for i in range(g):
    if s[n//g*i] != t[m//g*i]:
        print(-1)
        sys.exit()

print(l)

Submission status: Runtime Error

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,m = I()
s = v()
t = v()
l = lcm(n,m)
x = l//n
y = l//m
g = lcm(x,y)
a = 1

while len(s)>= g and len(t)>= g:
    if s[a-1] != t[a-1]:
        print(-1)
        sys.exit()
    a += g

print(l)

Submission status: Runtime Error

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,m = I()
s = v()
t = v()
l = lcm(n,m)
x = l//n
y = l//m
p = [-1]*l
k = 0
b = 0
for i in range(0,n,x):
    p[i]=s[k]
    k += 1

for j in range(0,m,y):
    if p[j] != -1 and p[j] != t[b]:
        print(-1)
        sys.exit()
    else:
        p[j]=t[b]
    b += 1

print(l)

Submission status: Runtime Error

Solution code:
import math

def lcm(x, y):
    return (x * y) // math.gcd(x, y)

N, M = map(int, input().split())
S = input()
T = input()
L = lcm(N , M)
x = [""] * L
for i in range(N):
    idx = i * (L // N)
    # x[idx] = S[i]
    print(x[idx])
for i in range(M):
    idx = i * (L // M)
    if x[idx] != "" and x[idx] != T[i]:
        print(-1)
        exit()
print(L)

Submission status: Runtime Error

Solution code:
import math

def lcm(x, y):
    return (x * y) // math.gcd(x, y)

N, M = map(int, input().split())
S = input()
T = input()
L = lcm(N , M)
x = [""] * L
for i in range(N):
    idx = i * (L // N)
    # x[idx] = S[i]
    print(S[i])
for i in range(M):
    idx = i * (L // M)
    if x[idx] != "" and x[idx] != T[i]:
        print(-1)
        exit()
print(L)

Submission status: Runtime Error

Solution code:
import math

def lcm(x, y):
    return (x * y) // math.gcd(x, y)

N, M = map(int, input().split())
S = input()
T = input()
L = lcm(N , M)
x = [""] * L
for i in range(N):
    idx = i * (L // N)
    x[idx] = S[i]
# for i in range(M):
#     idx = i * (L // M)
#     if x[idx] != "" and x[idx] != T[i]:
#         print(-1)
#         exit()
print(L)

Submission status: Runtime Error

Solution code:
import math

def lcm(x, y):
    return (x * y) // math.gcd(x, y)

N, M = map(int, input().split())
S = input()
T = input()
L = lcm(N , M)
x = [""] * (L + 10)
for i in range(N):
    idx = i * (L // N)
    x[idx] = S[i]
for i in range(M):
    idx = i * (L // M)
    if x[idx] != "" and x[idx] != T[i]:
        print(-1)
        exit()
print(L)

Submission status: Runtime Error

Solution code:
import math

def lcm(x, y):
    return (x * y) // math.gcd(x, y)

N, M = map(int, input().split())
S = input()
T = input()
L = lcm(N , M)
x = [""] * L
for i in range(N):
    idx = i * (L // N)
    x[idx] = S[i]
for i in range(M):
    idx = i * (L // M)
    if x[idx] != "" and x[idx] != T[i]:
        print(-1)
        exit()
print(L)

Submission status: Runtime Error

Solution code:
import math

def lcm(x, y):
    return (x * y) // math.gcd(x, y)

N, M = map(int, input().split())
S = input()
T = input()
L = lcm(N , M)
x = [0] * L
for i in range(N):
    idx = i * (L // N)
    x[idx] = S[i]
for i in range(M):
    idx = i * (L // M)
    if x[idx] != 0 and x[idx] != T[i]:
        print(-1)
        exit()
print(L)

Submission status: Runtime Error

Solution code:
import math

def lcm(x, y):
    return (x * y) // math.gcd(x, y)

N, M = map(int, input().split())
S = input()
T = input()
L = lcm(N , M)
x = [""] * (L)
for i in range(N):
    idx = i * (L // N)
    # x[idx] = S[i]
    print(x[idx])
for i in range(M):
    idx = i * (L // M)
    if x[idx] != "" and x[idx] != T[i]:
        print(-1)
        exit()
print(L)

Submission status: Runtime Error

Solution code:
import math

def lcm(x, y):
    return (x * y) // math.gcd(x, y)

N, M = map(int, input().split())
S = input()
T = input()
L = lcm(N , M)
x = [""] * (L)
for i in range(N):
    idx = i * (L // N)
    x[idx] = S[i]
print(0)
exit()
for i in range(M):
    idx = i * (L // M)
    if x[idx] != "" and x[idx] != T[i]:
        print(-1)
        exit()
print(L)

Submission status: Runtime Error

Solution code:
import math

def lcm(x, y):
    return (x * y) // math.gcd(x, y)

N, M = map(int, input().split())
S = input()
T = input()
L = lcm(N , M)
x = [""] * (L + 1)
for i in range(N):
    idx = i * (L // N)
    x[idx] = S[i]
for i in range(M):
    idx = i * (L // M)
    if x[idx] != "" and x[idx] != T[i]:
        print(-1)
        exit()
print(L)

Submission status: Runtime Error

Solution code:
import math

def lcm(x, y):
    return (x * y) // math.gcd(x, y)

N, M = map(int, input().split())
S = input()
T = input()
L = lcm(N , M)
X = [""] * L
for i in range(N):
    idx = i * (L // N)
    X[idx] = S[i]
for i in range(M):
    idx = i * (L // M)
    if X[idx] != "" and X[idx] != T[i]:
        print(-1)
        exit()
print(L)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')


def solve():
    n, m = [int(x) for x in input().split()]
    s = input()
    t = input()
    g = gcd(n, m)
    ans = n * m // g
    a, b = n // g, m // g
    x, y = 0, 0
    for i in range(g):
        if s[x] != x[y]:
            print(-1)
            return
        x += a
        y += b
    print(ans)
    
t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

2 2 2 2 2

"""

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')


def solve():
    n, m = [int(x) for x in input().split()]
    s = input()
    t = input()
    l = n * m // gcd(n, m)
    if n * m != l:
        if n > m:
            n, m = m, n
            s, t = t, s
        ok = 1
        for i in range(n):
            if s[i] != t[i * l // n]:
                ok = 0
        if ok:
            print(l)
        else:
            print(-1)
    else:
        print(l)

    




        
    

    
    

t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

2 2 2 2 2

"""

Submission status: Runtime Error

Solution code:
from math import *
a,b=map(int,input().split())
s=input()
t=input()
ans=a*b//gcd(a,b)
if a%b==0:
    if not all(s[i]==t[1+i*a//b] for i in range(b)):ans=-1
if b%a==0:
    if not all(t[i]==s[1+i*b//a] for i in range(a)):ans=-1
if s[0]!=t[0]:
    ans=-1
print(ans)

Submission status: Runtime Error

Solution code:
from math import *
a,b=map(int,input().split())
s=input()
t=input()
ans=a*b//gcd(a,b)
if a%b==0:
    if not all(s[i]==t[1+i*a//b] for i in range(b)):ans=-1
if b%a==0:
    if not all(t[i]==s[1+i*b//a] for i in range(a)):ans=-1
print(ans)

Submission status: Runtime Error

Solution code:
import sys,math
input = sys.stdin.readline

N,M = list(map(int,input().split()))
S = input().rstrip()
T = input().rstrip()

lcm = N*M//math.gcd(N,M)
X_S=list("#"*lcm)
for i in range(N):
    X_S[i*lcm//N] = S[i]

for i in range(M):
    if X_S[i*lcm//M] == T[i] or X_S[i*lcm//M] == '#':
        continue
    else:
        print(-1)
print(lcm)

Submission status: Runtime Error

Solution code:
import sys,math
input = sys.stdin.readline

N,M = list(map(int,input().split()))
S = input().rstrip()
T = input().rstrip()

lcm = N*M//math.gcd(N,M)
X_S=list("#"*lcm)
X_T=list("#"*lcm)
for i in range(N):
    X_S[i*lcm//N] = S[i]
for j in range(M):
    X_T[j*lcm//M] =T[j]

for i in range(lcm):
    if X_S[i] == X_T[i] or X_S[i] =='#' or X_T[i] =='#':
        continue
    else:
        print(-1)
        exit()
print(lcm)

Submission status: Runtime Error

Solution code:
from math import gcd
n, m, S, T = open(0).read().split()
n, m = int(n), int(m)
l = n*m // gcd(n, m)
A = ['*'] * l
step = l//n
for i in range(n):
    A[i*step] = S[i]
step = l//m
for i in range(m):
    if A[i*step] == T[i] or A[i*step] == '*':
        A[i*step] = T[i]
    else:
        print(-1)
        break
else:
    print(l)

Submission status: Runtime Error

Solution code:
from math import gcd
n, m, S, T = open(0).read().split()
n, m = int(n), int(m)
l = n*m // gcd(n, m)
A = ['*'] * l
step = l//n
for i in range(n):
    A[i*step] = S[i]
step = l//m
for i in range(m):
    if A[i*step] == T[i] or A[i*step] == '*':
        A[i*step] = T[i]
    else:
        print(-1)
else:
    print(l)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from math import gcd
N, M = map(int, input().split())
S = input()
T = input()
g = N//gcd(N, M)*M
ans = ['?']*g
for i in range(N):
    ans[i*g//N] = S[i]
ok = True
for i in range(M):
    idx = i*g//M
    if ans[idx] != '?' and ans[idx] != T[i]:
        ok = False
        break
if ok:
    print(g)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
from math import gcd
N, M = map(int, input().split())
S = input()
T = input()
g = N//gcd(N, M)*M
ans = ['?']*g
for i in range(N):
    ans[i*g//N] = S[i]
ok = True
for i in range(M):
    idx = i*g//M
    if ans[idx] != '?' and ans[idx] != T[i]:
        ok = False
        break
if ok:
    print(g)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n, m = MAP()
s = input()
t = input()
l = n*m//gcd(n,m)

for i in range(0,l,l-1):
        if (n*i) % l == (m*i) % l == 0 and s[n*i//l] != t[m*i//l]:
                print(-1)
                exit()
print(l)

Submission status: Runtime Error

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")

from collcections import defaultdict
import math
n,m = map(int, input().split())
s = input()
t = input()
g = math.gcd(n,m)
d = defaultdict(list)
for i in range(n):
    ind = i*(m//g)
    d[ind].append(s[i])
for i in range(m):
    ind = i*(n//g)
    d[ind].append(t[i])
for k,v in d.items():
    if v and not all(item==v[0] for item in v):
        ans = -1
        break
else:
    ans = n*m//g
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


import math
n,m = map(int, input().split())
s = input()
t = input()
g = math.gcd(n,m)
d = defaultdict(list)
for i in range(n):
    ind = i*(m//g)
    d[ind].append(s[i])
for i in range(m):
    ind = i*(n//g)
    d[ind].append(t[i])
for k,v in d.items():
    if v and not all(item==v[0] for item in v):
        ans = -1
        break
else:
    ans = n*m//g
print(ans)

Submission status: Runtime Error

Solution code:
from math import gcd
 
a,b=map(int, input().split())
c=input()
d=input()
 
e=gcd(n,m)
 
ae=a//e
be=b//e
 
for i in range(0,e):
    if c[ae*i]!=d[be*i]:
        print(-1)
        exit()
print(ae*be*e)

Submission status: Runtime Error

Solution code:
import fractions
import sys
input=sys.stdin.readline()
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
n,m=map(int,input().split())
s=input()
t=input()
if s[0]!=t[0]:
    print(-1)
    exit()
k=lcm(n,m)
ans=[""for i in range(k)]
ans[0]=s[0]
for i in range(1,n):
    ans[i*(k//n)]=s[i]
for i in range(1,m):
    if not ans[i*(k//m)]:
        ans[i*(k//m)]=t[i]
    else:
        if ans[i*(k//m)]!=t[i]:
            print(-1)
            exit()
print(k)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
s = input()
t = input()

def gcd(x, y):
    while y:
        x, y = y, x % y
    return x

def lcm(x, y):
    return x * y // gcd(x, y)

g = gcd(n, m)
bl = True
for k in range(g):
    i, j = k * n // g, k * m // g
    if s[i] != s[j]:
        bl = False

print(lcm(n, m)) if bl else print('-1')