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 = 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)

L = lcm(n, m)
target = [0] * L
for i in range(n):
    target[i * L // n] = s[i]
for i in range(m):
    if target[i * L // m] == 0 or target[i * L // m] == t[i]:
        target[i * L // m] = t[i]
    else:
        print('-1')
        exit()

print(L)

Submission status: Runtime Error

Solution code:
N, M = MAP()

S = input()
T = input()

if S[0] != T[0]:
	print(-1)
	exit()

G = gcd(N, M)

if G == 1:
	print(N*M)
	exit()

Submission status: Runtime Error

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(str, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

N, M = MAP()

S = input()
T = input()

L = N*M//gcd(N, M)
check = ["_"]*L
check[0] = S[0]

for i in range(N):
	check[L//N * i] = S[i]

for j in range(M):
	if check[L//M * j] != "_" and check[L//M * j] != T[j]:
		print(-1)
		break
else:
	print(L)

Submission status: Runtime Error

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

from math import gcd

lcm=n*m//gcd(n,m)

if s[0]!=t[0]:
    print(-1)
    exit(0)

ans=lcm
# sについて
dn={}
for i in range(n):
    if i==0:
        dn[1]=s[i]
    else:
        dn[i*int((lcm)/n) +1]=s[i]

for j in range(m):
    if j==0:
        continue
    else:
        k = j*int(lcm/m) + 1

        if k in dn:
            if dn[k]!=t[j]:
                ans=-1
        else:
            dn[k]=t[j]

print(ans)

Submission status: Runtime Error

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import deque
import pprint

inf = 10**18
mod = 10**9+7

n,m = map(int, input().split())
s = input()
t = input()
f=math.gcd(n,m)           #最大公約数
f2=n*m//f                          #最小公倍数


x = f2//n
y = f2//m

z = ['' for i in range(f2)]
for i in range(n):
    z[i*x] = s[i]
for i in range(m):
    if z[i*y] != '' and z[i*y] != t[i]:
        print(-1)
        sys.exit()
print(f2)

Submission status: Runtime Error

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# GCD -- START --
def gcd(x,y):
  while y:
    x,y=y,x%y
  return x
# GCD --- END ---

# LCM -- START --
def lcm(x,y):
  return x*y//gcd(x,y)
# LCM --- END ---

def main():
  n,m=LI()
  s=S()
  t=S()
  a=lcm(n,m)

  y=['a']*(10**10)

  x=['.']*a
  for i in range(n):
    x[i*(a//n)]=s[i]

  # print(x)

  for i in range(m):
    if x[i*(a//m)]!='.' and x[i*(a//m)]!=t[i]:
      return -1

  return a

# main()
print(main())

Submission status: Runtime Error

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# GCD -- START --
def gcd(x,y):
  while y:
    x,y=y,x%y
  return x
# GCD --- END ---

# LCM -- START --
def lcm(x,y):
  return x*y//gcd(x,y)
# LCM --- END ---

def main():
  n,m=LI()
  s=S()
  t=S()
  a=lcm(n,m)

  x=['.']*a
  for i in range(n):
    x[i*(a//n)]=s[i]

  # print(x)

  for i in range(m):
    if x[i*(a//m)]!='.' and x[i*(a//m)]!=t[i]:
      return -1

  return a

# main()
print(main())

Submission status: Runtime Error

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

from fractions import gcd   #for python3.4
from math import gcd        #for python3.8

def lcm(a,b):
    G = gcd(a, b) #最大公約数
    L = (a//G)*b #最小公倍数
    return L

def main():
    N, M = map(int, input().split())
    S = input()
    T = input()
    L = lcm(N,M)
    if S[::L//M]==T[::L//N]:
        print(L)
    else:
        print(-1)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import fractions

def lcm(x,y):
    return (x*y) // fractions.gcd(x, y)
    
x, y = map(int, input().split())
s = input()
t = input()

check = [""] * (10**11)
lc = lcm(x,y)

s_l = lc//x
t_l = lc//y

for i in range(x):
    check[i*s_l + 1] = s[i]
    
for i in range(y):
    if check[i*t_l + 1] != t[i] and check[i*t_l + 1] != "":
        print(-1)
        break
else:
    print(lc)

Submission status: Runtime Error

Solution code:
import fractions

def lcm(x,y):
    return (x*y) // fractions.gcd(x, y)
    
x, y = map(int, input().split())
s = input()
t = input()

check = [""] * (10**6)
lc = lcm(x,y)

s_l = lc//x
t_l = lc//y

for i in range(x):
    check[i*s_l + 1] = s[i]
    
for i in range(y):
    if check[i*t_l + 1] != t[i] and check[i*t_l + 1] != "":
        print(-1)
        break
else:
    print(lc)

Submission status: Runtime Error

Solution code:
import math

def lcm(x,y):
    return (x*y) / math.gcd(x, y)
    
x, y = map(int, input().split())
s = input()
t = input()

check = [""] * (10**6)
lc = lcm(x,y)

s_l = lc/x
t_l = lc/y

for i in range(x):
    check[i*s_l + 1] = s[i]
    
for i in range(y):
    if check[i*t_l + 1] != t[i] and check[i*t_l + 1] != "":
        print(-1)
        break
else:
    print(lc)

Submission status: Runtime Error

Solution code:
import math

def lcm(x,y):
    return (x*y) // math.gcd(x, y)
    
x, y = map(int, input().split())
s = input()
t = input()

check = [""] * (10**6)
lc = lcm(x,y)

s_l = lc//x
t_l = lc//y

for i in range(x):
    check[i*s_l + 1] = s[i]
    
for i in range(y):
    if check[i*t_l + 1] != t[i] and check[i*t_l + 1] != "":
        print(-1)
        break
else:
    print(lc)

Submission status: Runtime Error

Solution code:
import math

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

if N == M:
    print(N if S == T else -1)
else:
    G = math.gcd(N, M)
    L = N * M // G
    n = N // G
    m = M // G
    ans = True
    for k in range(G):
        if S[k * n] != T[k * m]:
            ans = False
            break
    print(L if ans else -1)

Submission status: Runtime Error

Solution code:
from math import gcd

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

G = gcd(N, M)
L = N * M // G
n = N // G
m = M // G

ans = True
for k in range(G):
    if S[k * n] != T[k * m]:
        ans = False
        break
print(L if ans else -1)

Submission status: Runtime Error

Solution code:
import fractions

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

n,m = map(int,input().split())
S = input()
T = input()

l = lcm(n,m)
c_max = max(n,m)
ans_s = [" "]*(l+1)
ans_t = [" "]*(l+1)
for i in range(n):
    ans_s[i*l//n+1] = S[i]
for i in range(m):
    ans_t[i*l//m+1] = T[i]
    
ans = l
for i in range(l+1):
    if ans_s[i] != " " and ans_t[i] != " " and ans_s[i] != ans_t[i]:
        ans = -1

print(ans)

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)
c_max = max(n,m)
ans_s = [" "]*(l+1)
ans_t = [" "]*(l+1)
for i in range(n):
    ans_s[i*l//n+1] = S[i]
for i in range(m):
    ans_t[i*l//m+1] = T[i]
    
ans = l
for i in range(l+1):
    if ans_s[i] != " " and ans_t[i] != " " and ans_s[i] != ans_t[i]:
        ans = -1

print(ans)

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)
c_max = max(n,m)
ans_s = [" "]*(l+1)
ans_t = [" "]*(l+1)
for i in range(n):
    ans_s[i*l//n+1] = S[i]
for i in range(m):
    ans_t[i*l//m+1] = T[i]
    
ans = l
for i in range(l+1):
    if ans_s[i] != " " and ans_t[i] != " " and ans_s[i] != ans_t[i]:
        ans = -1

print(ans)

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/agc028/tasks/agc028_a

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

def gcd(x, y):
    if x < y:
        x, y = y, x
    if y == 0:
        return 0
    if x % y == 0:
        return y
    return gcd(y, x % y)

l = n * m // gcd(n, m)
x = [''] * (l)
ans = l
for i in range(n):
    p = i * (l // n)
    x[p] = s[i]
for i in range(m):
    p = i * (l // m)
    if x[p] == '':
        x[p] = t[i]
    else:
        if x[p] == t[i]:
            continue
        else:
            ans = -1
            break
print(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: S[0]}
for i in range(1, N):
    c_num = i*l//N+1
    ans[c_num] = S[i]

for i in range(M):
    if i == 1:
        if ans[1] != T[0]:
            print(-1)
            exit(0)

    c_num = i*l//M+1
    if c_num in ans:
        if ans[c_num] != T[i]:
            print(-1)
            exit(0)
    else:
        ans[c_num] = T[i]
print(l)

Submission status: Runtime Error

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

import sys

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

g = gcd(N, M)
sn = N/g
sm = M/g

ok = True
for k in range(0, g):
    a = k * sn
    b = k * sm
    if S[a] != T[b]:
        ok = False
        break

if ok:
    print(N*M/g)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**6)
    input = sys.stdin.readline

    def gcd(a, b):
        if b == 0:
            return a
        return gcd(b, a % b)
    
    N, M = [int(x) for x in input().strip().split()]
    S = input().strip()
    T = input().strip()
    L = (N * M) // gcd(N, M)
    ans = ['0'] * L
    for n in range(N):
        ans[L//N*n] = S[n]
    for m in range(M):
        if ans[(L//M)*m] == '0':
            ans[(L//M)*m] = T[m]
        elif ans[(L//M)*m] == T[m]:
            continue
        else:
            print('-1')
            return
    print(L)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**6)
    input = sys.stdin.readline

    def gcd(a, b):
        if b == 0:
            return a
        return gcd(b, a % b)
    
    N, M = [int(x) for x in input().strip().split()]
    S = input().strip()
    T = input().strip()
    L = (N * M) // gcd(N, M)
    ans = ['0'] * L
    for n in range(L//N):
        ans[L//N*n] = S[n%N]
    for m in range(L//M):
        if ans[(L//M)*m] == '0':
            ans[(L//M)*m] = T[m%M]
        elif ans[(L//M)*m] == T[m%M]:
            continue
        else:
            print(-1)
            return
    print(L)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**6)
    input = sys.stdin.readline

    def gcd(a, b):
        if b == 0:
            return a
        return gcd(b, a % b)
    
    N, M = [int(x) for x in input().strip().split()]
    S = input().strip()
    T = input().strip()
    L = (N * M) // gcd(N, M)
    ans = ['0'] * L
    for n in range(N):
        ans[L//N*n] = S[n%N]
    for m in range(M):
        if ans[(L//M)*m] == '0':
            ans[(L//M)*m] = T[m%M]
        elif ans[(L//M)*m] == T[m%M]:
            continue
        else:
            print(-1)
            return
    print(L)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    def gcd(a, b):
        if b == 0:
            return a
        return gcd(b, a % b)
    
    N, M = [int(x) for x in input().strip().split()]
    S = input().strip()
    T = input().strip()
    L = ((N * M) // gcd(N, M))
    ans = ['0'] * L
    for n in range(N):
        ans[L//N*n] = S[n%N]
    for m in range(M):
        if ans[(L//M)*m] == '0':
            ans[(L//M)*m] = T[m%M]
        elif ans[(L//M)*m] == T[m%M]:
            continue
        else:
            print(-1)
            return
    print(L)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    def gcd(a, b):
        if b == 0:
            return a
        return gcd(b, a % b)
    
    N, M = [int(x) for x in input().strip().split()]
    S = input().strip()
    T = input().strip()
    L = ((N * M) // gcd(N, M))
    ans = ['0'] * L
    for n in range(N):
        ans[L//N*n] = S[n]
    for m in range(M):
        if ans[(L//M)*m] == '0':
            ans[(L//M)*m] = T[m]
        elif ans[(L//M)*m] == T[m]:
            continue
        else:
            print(-1)
            return
    print(L)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from fractions import gcd


N, M = map(int, input().split())
S = input()
T = input()
LCM = N * M // gcd(N, M)
S_dist = [""] * LCM
T_dist = [""] * LCM
for i in range(N):
    S_dist[i * LCM // N] = S[i]
for i in range(M):
    T_dist[i * LCM // M] = T[i]
for s, t in zip(S_dist, T_dist):
    if s != "" and t != "" and s != t:
        print(-1)
        break
else:
    print(LCM)

Submission status: Runtime Error

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
from math import gcd

n,m = iim()
s = input()
t = input()
num = gcd(n,m)
lcm = n*m//num
print(lcm if s[::n//num] == t[::m//num] else -1)

Submission status: Runtime Error

Solution code:
from fractions import gcd


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


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

L = lcm(N, M)
X = {}

for i in range(0, L, L // N):
    X[i] = S[i]

for i in range(0, L, L // M):
    if i not in X:
        continue
    if X[i] != T[i]:
        print(-1)
        exit()
print(L)

Submission status: Runtime Error

Solution code:
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

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

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

ans = lcm(n, m)

chk = [None]*ans

for i in range(n):
    d = ans//n
    chk[i*d] = s[i]
for i in range(m):
    d = ans//m
    target = chk[i*d]
    if target is not None and target != t[i]:
        print(-1)
        exit()
print(ans)

Submission status: Runtime Error

Solution code:
import math

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


dic = {i+1: [] for i in range(int((N*M) / math.gcd(N,M)))}
L = len(dic)
ans = L

for n in range(N):
  k = int(n*(L/N))+1
  #print(k)
  dic[k].append(S[n])

#print(dic)
for m in range(M):
  k = int(m * (L/M) + 1)
  if len(dic[k]) == 0:continue
  else: 
    #print(T[m],dic[k])
    if dic[k][0] != T[m]:
      ans = -1
      break


print(ans)

Submission status: Runtime Error

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

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()

l = lcm(N, M)

Moji = ['']*l
Moji[0] = S[0]

for i in range(1, N):
    index = i * (l//N) + 1
    if Moji[index-1] == '':
        Moji[index-1] = S[i]

for i in range(1, M):
    index = i * (l//M) + 1
    if Moji[index-1] == '':
        Moji[index-1] = T[i]
    else:
        if Moji[index-1] != T[i]:
            print(-1)
            exit()

print(l)

Submission status: Runtime Error

Solution code:
#import fractions
import sys
sys.setrecursionlimit(10**6)

def euclid(x,y):
    if x < y:
        tmp = x
        x = y
        y = tmp
    if y == 0:
        return x
    return euclid(y,x%y)

[n,m] = [int(i) for i in input().split()]
s = input()
t = input()

#l = n * m // euclid(n,m)
l = n * m
ls = ["0"] * l
flg = 1

for i in range(n):
    ls[i * l // n] = s[i]
#print(l)

for i in range(m):
    if ls[i * l // m] != "0" and ls[i * l // m] != t[i]:
        flg = 0

if flg:
    print(l)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
#import fractions
import sys
sys.setrecursionlimit(10**6)

def euclid(x,y):
    if x < y:
        tmp = x
        x = y
        y = tmp
    if y == 0:
        return x
    return euclid(y,x%y)

[n,m] = [int(i) for i in input().split()]
s = input()
t = input()



l = n * m // euclid(n,m)
ls = ["0"] * l
flg = 1

for i in range(n):
    ls[i * l // n] = s[i]
#print(l)

for i in range(m):
    if ls[i * l // m] != "0" and ls[i * l // m] != t[i]:
        flg = 0

if flg:
    print(l)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
from fractions import gcd

n, m = map(int, input().split())

s = input()
t = input()

# accept
# a c p
# a  e

# 最小公倍数。被ったらNG
l = n * m // gcd(n, m)
x = [None] * l
for i, c in enumerate(s):
    if i*l//n >= l:
        break
    x[i*l//n] = c

for i, c in enumerate(t):
    if i*l//m >= l:
        break
    
    a = x[i*l//m]
    if a is None:
        continue
    elif a != c:
        print(-1)
        exit()

print(l)

Submission status: Runtime Error

Solution code:
from fractions import gcd

n, m = map(int, input().split())

s = input()
t = input()

# accept
# a c p
# a  e

# 最小公倍数。被ったらNG
l = n * m // gcd(n, m)
x = [None] * (n*m)
for i, c in enumerate(s):
    x[i*l//n] = c

for i, c in enumerate(t):
    a = x[i*l//m]
    if a is None:
        continue
    elif a != c:
        print(-1)
        exit()

print(l)

Submission status: Runtime Error

Solution code:
from fractions import gcd

n, m = map(int, input().split())

s = input()
t = input()

# accept
# a c p
# a  e

# 最小公倍数。被ったらNG
l = n * m // gcd(n, m)
x = [None] * l
for i, c in enumerate(s):
    x[i*l//n] = c

for i, c in enumerate(t):
    a = x[i*l//m]
    if a is None:
        continue
    elif a != c:
        print(-1)
        exit()

print(l)

Submission status: Runtime Error

Solution code:
import fractions

[n,m] = [int(i) for i in input().split()]
s = input()
t = input()

l = (n * m) // fractions.gcd(n,m)
ls = ["0"] * l
flg = 1

for i in range(n):
    ls[i * l // n] = s[i]
#print(l)

for i in range(m):
    if ls[i * l // m] != "0" and ls[i * l // m] != t[i]:
        flg = 0

if flg:
    print(l)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import fractions

[n,m] = [int(i) for i in input().split()]
s = input()
t = input()

l = n * m // fractions.gcd(n,m)
ls = ["0"] * l
flg = 1
"""
for i in range(n):
    ls[i * l // n] = s[i]
print(l)

for i in range(m):
    if ls[i * l // m] != "0" and ls[i * l // m] != t[i]:
        flg = 0

if flg:
    print(l)
else:
    print(-1)
"""

Submission status: Runtime Error

Solution code:
import fractions

[n,m] = [int(i) for i in input().split()]
s = input()
t = input()

l = n * m // fractions.gcd(n,m)
ls = ["0"] * l
flg = 1

for i in range(n):
    ls[i * l // n] = s[i]
print(l)
"""
for i in range(m):
    if ls[i * l // m] != "0" and ls[i * l // m] != t[i]:
        flg = 0

if flg:
    print(l)
else:
    print(-1)
"""

Submission status: Runtime Error

Solution code:
import fractions

[n,m] = [int(i) for i in input().split()]
s = input()
t = input()

l = n * m // fractions.gcd(n,m)
ls = ["0"] * (l+5)
flg = 1

for i in range(n):
    ls[i * l // n] = s[i]
for i in range(m):
    if ls[i * l // m] != "0" and ls[i * l // m] != t[i]:
        flg = 0

if flg:
    print(l)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import fractions
[n,m] = [int(i) for i in input().split()]
s = input()
t = input()

l = n * m // fractions.gcd(n,m)
ls = ["0"] * l
flg = 1
for i in range(n):
    ls[i * l // n] = s[i]
for i in range(m):
    if ls[i * l // m] != "0" and  ls[i * l // m] != t[i]:
        flg = 0

if flg:
    print(l)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import math
[n,m] = [int(i) for i in input().split()]
s = input()
t = input()

l = n * m // math.gcd(n,m)
ls = ["0"] * l
flg = 1
for i in range(n):
    ls[i * l // n] = s[i]
for i in range(m):
    if ls[i * l // m] != "0" and  ls[i * l // m] != t[i]:
        flg = 0

if flg:
    print(l)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
from fractions import gcd
n, m = map(int, input().split())
s = list(input())
t = list(input())
i = 1
ans = n * m // gcd(n, m)

a = [""] * (ans + 1)
flag = False
for j in range(n):
    a[ans // n * j] = s[j]
for k in range(m):
    if a[ans // m * k] != "" and a[ans // m * k] != t[k]:
        i += 1
        flag = True
        break
    a[ans // m * k] = t[k]
if flag:
    ans = -1

print(ans)

Submission status: Runtime Error

Solution code:
from fractions import gcd
n, m = map(int, input().split())
s = list(input())
t = list(input())
i = 1
ans = n * m // gcd(n, m)

a = [""] * (ans * i)
flag = False
for j in range(n):
    a[ans // n * j] = s[j]
for k in range(m):
    if a[ans // m * k] != "" and a[ans // m * k] != t[k]:
        i += 1
        flag = True
        break
    a[ans // m * k] = t[k]
if flag:
    ans = -1

print(ans)

Submission status: Runtime Error

Solution code:
from fractions import gcd
n, m = map(int, input().split())
s = list(input())
t = list(input())
if max(n, m) % min(n, m) == 0:
    print(-1)
else:
    i = 1
    lcm = n * m // gcd(n, m)
    while 1:
        a = [""] * (lcm * i)
        flag = True
        for j in range(n):
            a[lcm // n * j] = s[j]
        for k in range(m):
            if a[lcm // m * k] != "" and a[lcm // m * k] != t[k]:
                i += 1
                flag = False
                break
            a[lcm // m * k] = t[k]
        if flag:
            print(lcm * i)
            exit()

Submission status: Runtime Error

Solution code:
from math import gcd
n, m = map(int, input().split())
s = input()
t = input()
if max(n, m) % min(n, m) == 0:
    print(-1)
else:
    print(n * m // gcd(n, m))

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

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

n, m = map(int, input().split())
s = str(input())
t = str(input())
l = lcm(n, m)
L = [0] * l
for i in range(n):
    L[i*(l//n)] = s[i]
for j in range(m):
    if L[j*(l//m)] == 0:
        L[j*(l//m)] = t[j]
    elif L[j*(l//m)] == t[j]:
        pass
    else:
        print(-1)
        break
else:
    print(l)

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

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

n, m = map(int, input().split())
s = str(input())
t = str(input())
l = lcc(n, m)
L = [0] * l
for i in range(n):
    L[i*(l//n)] = s[i]
for j in range(m):
    if L[j*(l//m)] == 0:
        L[j*(l//m)] = t[j]
    elif L[j*(l//m)] == t[j]:
        pass
    else:
        print(-1)
        break
else:
    print(l)

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

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

n, m = map(int, input().split())
s = str(input())
t = str(input())
g = gcd(n, m)
l = lcm(n, m)
L = [0] * l
for i in range(g):
    if s[i*n//g] != t[i*m//g]:
        print(-1)
        break
else:
    print(l)

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

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

n, m = map(int, input().split())
s = str(input())
t = str(input())
l = lmc(n, m)
L = [0] * l
for i in range(n):
    L[i*(l//n)] = s[i]
for j in range(m):
    if L[j*(l//m)] == 0:
        L[j*(l//m)] = t[j]
    elif L[j*(l//m)] == t[j]:
        pass
    else:
        print(-1)
        break
else:
    print(l)

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

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

n, m = map(int, input().split())
s = str(input())
t = str(input())
l = lmc(n, m)
L = [0] * l
for i in range(n):
    L[i*(l//n)] = s[i]
for j in range(m):
    if L[j*(l//m)] == 0 or L[j*(l//m)] == t[j]:
        L[j*(l//m)] = t[j]
    else:
        print(-1)
        break
else:
    print(l)

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

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

n, m = map(int, input().split())
s = str(input())
t = str(input())
l = lmc(n, m)
L = [0] * l
for i in range(0, n, l//n):
    L[i] = s[i//(l//n)]
for j in range(0, m, l//m):
    if L[j] == 0 or L[j] == t[j//(l//m)]:
        L[j] = t[j//(l//m)]
    else:
        print(-1)
        break
else:
    print(l)

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

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

n, m = map(int, input().split())
s = str(input())
t = str(input())
l = lmc(n, m)
L = [0] * l
for i in range(0, n, l//n):
    L[i] = s[i]
for j in range(0, m, l//m):
    if L[j] == 0 or L[j] == t[j//(l//m)]:
        L[j] = t[j//(l//m)]
    else:
        print(-1)
        break
else:
    print(l)

Submission status: Runtime Error

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

def answer(s, t):
    a = len(s)
    b = len(t)
    if a[0] != b[0]:
        return -1
    if a < b:
        temp = a
        a = b
        b = temp
    gcdAB = gcd(a, b)
    if gcdAB == 1:
        return a*b
    else:
        stack = 0
        sl = (gcdAB * (a//gcdAB) * (b//gcdAB))
        aJump = sl//a
        bJump = sl//b
        for i in range(b):
            if stack%aJump == 0:
                if s[stack//aJump] != t[i]:
                    return -1
            stack = bJump*(i+1)
        return sl
                

sl, tl = map(int, input().split())
s = input()
t = input()
print(answer(s, t))

Submission status: Runtime Error

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

def answer(s, t):
    a = len(s)
    b = len(t)
    if a < b:
        temp = a
        a = b
        b = temp
    gcdAB = gcd(a, b)
    if gcdAB == 1:
        return a*b
    else:
        stack = 0
        sl = (gcdAB * (a//gcdAB) * (b//gcdAB))
        for i in range(gcdAB):
            if s[stack//gcdAB] != t[stack//(sl//b)]:
                return -1
            stack += a
        return a*gcdAB

sl, tl = map(int, input().split())
s = input()
t = input()
print(answer(s, t))

Submission status: Runtime Error

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,M=MI()
    S=ST()
    T=ST()
    lcm = N*M//(gcd(N,M))
    ans = ["?"]*lcm
    for i,s in enumerate(S):
        ans[i*lcm//N] = s
    for i,t in enumerate(T):
        if(ans[i*lcm//M] not in (t,"?")):
            print(-1)
            return 
    print(lcm)
if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

C=N*M//gcd(N,M)
n=C//N
m=C//M

i=0
j=0
ans=C
while i<C and j<C:
    if  i==j:
        if S[i//n]!=T[j//m]:
            ans=-1
        i+=n
        j+=m
    elif i<j:
        i+=n
    else:
        j+=m
print(ans)

Submission status: Runtime Error

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

def answer(s, t):
    a = len(s)
    b = len(t)
    if a < b:
        temp = a
        a = b
        b = temp
    gcdAB = gcd(a, b)
    if gcdAB == 1:
        return a*b
    else:
        stack = 0
        c = (a*gcdAB)//b
        for i in range(b):
            if s[i+stack] != t[i]:
                return -1
            stack += c
        return a*gcdAB

sl, tl = map(int, input().split())
s = input()
t = input()
print(answer(s, t))