Problem p03231 (python)

Fixed submission:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
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 = defaultdict(str)
    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()

Buggy submission (verdict: Runtime Error):
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
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()

Buggy submission (verdict: Runtime Error):
from fractions import gcd
N, M = map(int, input().split())
S = input()
T = input()
gcd_ = gcd(N, M)
lcm = N * M // gcd_
s_ = lcm//N
t_ = lcm // M

X = ["0"] * (lcm*2)
for i in range(N):
    #print(i, i * s_)
    X[i * s_] = S[i]
ans = True
for i in range(M):
    if X[i * t_] != "0" and T[i] != X[i * t_]:
        ans = False
        break
    else:
        X[i * t_] = T[i]
if ans:
    print(lcm)
else:
    print("-1")
"""
ans = True
if gcd_ != 1:
    for i in range(gcd_):
        # print(i)
        if S[s_ * i] != T[t_ * i]:
            ans = False
            # break
if ans:
    print(lcm)
else:
    print(-1)
"""

Buggy submission (verdict: Runtime Error):
from fractions import gcd
N, M = map(int, input().split())
S = input()
T = input()
gcd_ = gcd(len(S), len(T))
saisho = len(S) * len(T) // gcd_
s_ = saisho//len(S)
t_ = saisho//len(T)

X = ["0"] * (saisho*2)
for i in range(len(S)):
    #print(i, i * s_)
    X[i * s_] = S[i]
ans = True
for i in range(len(T)):
    if X[i * t_] != "0" and T[i] != X[i * t_]:
        ans = False
    else:
        X[i * t_] = T[i]
if ans:
    print(saisho)
else:
    print("-1")
"""
ans = True
if gcd_ != 1:
    for i in range(gcd_):
        # print(i)
        if S[s_ * i] != T[t_ * i]:
            ans = False
            # break
if ans:
    print(saisho)
else:
    print(-1)
"""

Buggy submission (verdict: Runtime Error):
from fractions import gcd
N, M = map(int, input().split())
S = input()
T = input()
gcd_ = gcd(len(S), len(T))
saisho = len(S) * len(T) // gcd_
s_ = saisho//len(S)
t_ = saisho//len(T)

X = ["0"] * (saisho+1)
for i in range(len(S)):
    #print(i, i * s_)
    X[i * s_] = S[i]
ans = True
for i in range(len(T)):
    if X[i * t_] != "0" and T[i] != X[i * t_]:
        ans = False
    else:
        X[i * t_] = T[i]
if ans:
    print(saisho)
else:
    print("-1")
"""
ans = True
if gcd_ != 1:
    for i in range(gcd_):
        # print(i)
        if S[s_ * i] != T[t_ * i]:
            ans = False
            # break
if ans:
    print(saisho)
else:
    print(-1)
"""

Buggy submission (verdict: Runtime Error):
from fractions import gcd
N, M = map(int, input().split())
S = input()
T = input()
gcd_ = gcd(len(S), len(T))
saisho = len(S) * len(T) // gcd_
s_ = saisho//len(S)
t_ = saisho//len(T)

X = ["0"] * saisho
for i in range(len(S)):
    #print(i, i * s_)
    X[i * s_] = S[i]
ans = True
for i in range(len(T)):
    if X[i * t_] != "0" and T[i] != X[i * t_]:
        ans = False
    else:
        X[i * t_] = T[i]
if ans:
    print(saisho)
else:
    print("-1")
"""
ans = True
if gcd_ != 1:
    for i in range(gcd_):
        # print(i)
        if S[s_ * i] != T[t_ * i]:
            ans = False
            # break
if ans:
    print(saisho)
else:
    print(-1)
"""