Buggy python submission (verdict: Time Limit Exceeded):
import sys
import fractions
N, M = map(int, input().split())
S = input()
T = input()

g = fractions.gcd(N, M)
L = N * M // g

ss = L // N
ts = L // M
for i in range(L):
  if i % ss == 0 and i % ts == 0:
    if S[i//ss] != T[i//ts]:
      print(-1)
      sys.exit()
print(L)

Fixed submission:
import sys
import fractions
N, M = map(int, input().split())
S = input()
T = input()

g = fractions.gcd(N, M)
L = N * M // g

ss = L // N
ts = L // M
for i in range(N):
  if (i * ss) % ts == 0:
    j = (i * ss) // ts
    if S[i] != T[j]:
      print(-1)
      sys.exit()

for i in range(M):
  if (i * ts) % ss == 0:
    j = (i * ts) // ss
    if T[i] != S[j]:
      print(-1)
      sys.exit()

print(L)

Buggy python submission (verdict: Runtime Error):
import fractions

N, M = map(int, input().split())
S = input()
T = input()
L = N*M//fractions.gcd(N, M)

try:
  XS = [' ']*L
  XT = [' ']*L
except:
  L = L // 10
  XS = [' ']*L
  XT = [' ']*L

for i in range(N):
  idx = L//N*i
  try:
    XS[idx] = S[i]
  except:
    pass

for i in range(M):
  idx = L//M*i
  try:
    XT[idx] = T[i]
  except:
    pass

  if XS[i] == ' ' or XT[i] == ' ':
    continue
  elif XS[i] == XT[i]:
    continue
  else:
    print(-1)
    exit()

print(L)

Fixed submission:
import sys
import fractions
N, M = map(int, input().split())
S = input()
T = input()

g = fractions.gcd(N, M)
L = N * M // g

ss = L // N
ts = L // M
for i in range(N):
  if (i * ss) % ts == 0:
    j = (i * ss) // ts
    if S[i] != T[j]:
      print(-1)
      sys.exit()

for i in range(M):
  if (i * ts) % ss == 0:
    j = (i * ts) // ss
    if T[i] != S[j]:
      print(-1)
      sys.exit()

print(L)

Buggy python submission (verdict: Runtime Error):
import fractions

N, M = map(int, input().split())
S = input()
T = input()
L = N*M//fractions.gcd(N, M)

XS = [' ']*L
XT = [' ']*L

for i in range(N):
  idx = L//N*i
  try:
    XS[idx] = S[i]
  except:
    pass

for i in range(M):
  idx = L//M*i
  try:
    XT[idx] = T[i]
  except:
    pass

  if XS[i] == ' ' or XT[i] == ' ':
    continue
  elif XS[i] == XT[i]:
    continue
  else:
    print(-1)
    exit()

print(L)

Fixed submission:
import sys
import fractions
N, M = map(int, input().split())
S = input()
T = input()

g = fractions.gcd(N, M)
L = N * M // g

ss = L // N
ts = L // M
for i in range(N):
  if (i * ss) % ts == 0:
    j = (i * ss) // ts
    if S[i] != T[j]:
      print(-1)
      sys.exit()

for i in range(M):
  if (i * ts) % ss == 0:
    j = (i * ts) // ss
    if T[i] != S[j]:
      print(-1)
      sys.exit()

print(L)

Buggy python submission (verdict: Runtime Error):
import fractions

N, M = map(int, input().split())
S = input()
T = input()
L = N*M//fractions.gcd(N, M)

XS = [' ']*L
XT = [' ']*L

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

for i in range(M):
  idx = L//M*i
  XT[idx] = T[i]

for i in range(L//2):
  if XS[i] == ' ' or XT[i] == ' ':
    continue
  elif XS[i] == XT[i]:
    continue
  else:
    print(-1)
    exit()

print(L)

Fixed submission:
import sys
import fractions
N, M = map(int, input().split())
S = input()
T = input()

g = fractions.gcd(N, M)
L = N * M // g

ss = L // N
ts = L // M
for i in range(N):
  if (i * ss) % ts == 0:
    j = (i * ss) // ts
    if S[i] != T[j]:
      print(-1)
      sys.exit()

for i in range(M):
  if (i * ts) % ss == 0:
    j = (i * ts) // ss
    if T[i] != S[j]:
      print(-1)
      sys.exit()

print(L)

Buggy python submission (verdict: Runtime Error):
import fractions

N, M = map(int, input().split())
S = input()
T = input()
L = N*M//fractions.gcd(N, M)

XS = [' ']*L
XT = [' ']*L

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

for i in range(M):
  idx = L//M*i
  XT[idx] = T[i]

for i in range(L):
  if XS[i] == ' ' or XT[i] == ' ':
    continue
  elif XS[i] == XT[i]:
    continue
  else:
    print(-1)
    exit()

print(L)

Fixed submission:
import sys
import fractions
N, M = map(int, input().split())
S = input()
T = input()

g = fractions.gcd(N, M)
L = N * M // g

ss = L // N
ts = L // M
for i in range(N):
  if (i * ss) % ts == 0:
    j = (i * ss) // ts
    if S[i] != T[j]:
      print(-1)
      sys.exit()

for i in range(M):
  if (i * ts) % ss == 0:
    j = (i * ts) // ss
    if T[i] != S[j]:
      print(-1)
      sys.exit()

print(L)

Buggy python submission (verdict: Runtime Error):
import fractions

N, M = map(int, input().split())
S = input()
T = input()
L = N*M//fractions.gcd(N, M)

def S_ni_naru(X_S, S, L, N):
    for i in range(N):
        idx = L//N*i
        X_S[idx] = S[i]
    return X_S


def T_ni_naru(X_T, T, L, M):
    for i in range(M):
        idx = L//M*i
        X_T[idx] = T[i]
    return X_T

X_S = S_ni_naru([0]*L, S, L, N)
X_T = T_ni_naru([0]*L, T, L, M)

for s, t in zip(X_S, X_T):
    if (s != t) and (s != 0) and (t != 0):
        print(-1)
        exit()

print(L)


Fixed submission:
import sys
import fractions
N, M = map(int, input().split())
S = input()
T = input()

g = fractions.gcd(N, M)
L = N * M // g

ss = L // N
ts = L // M
for i in range(N):
  if (i * ss) % ts == 0:
    j = (i * ss) // ts
    if S[i] != T[j]:
      print(-1)
      sys.exit()

for i in range(M):
  if (i * ts) % ss == 0:
    j = (i * ts) // ss
    if T[i] != S[j]:
      print(-1)
      sys.exit()

print(L)

Buggy python submission (verdict: Runtime Error):
import fractions


def main():
    X_S = S_ni_naru([0]*L, S, L, N)
    X_T = T_ni_naru([0]*L, T, L, M)

    for s, t in zip(X_S, X_T):
        if (s != t) and (s != 0) and (t != 0):
            print(-1)
            exit()

    print(L)


def S_ni_naru(X_S, S, L, N):
    s_i = 0
    for i in range(N):
        if i == 0:
            idx = 0
        else:
            idx = min(L//N*i, len(X_S)-1)

        X_S[idx] = S[s_i]
        s_i = s_i + 1

    return X_S


def T_ni_naru(X_T, T, L, M):
    t_i = 0
    for i in range(M):
        if i == 0:
            idx = 0
        else:
            idx = min(L//M*i, len(X_T)-1)

        X_T[idx] = T[t_i]
        t_i = t_i + 1

    return X_T


if __name__ == "__main__":
    N, M = map(int, input().split())
    S = input()
    T = input()
    L = int((N*M) / fractions.gcd(N, M))
    main()


Fixed submission:
import sys
import fractions
N, M = map(int, input().split())
S = input()
T = input()

g = fractions.gcd(N, M)
L = N * M // g

ss = L // N
ts = L // M
for i in range(N):
  if (i * ss) % ts == 0:
    j = (i * ss) // ts
    if S[i] != T[j]:
      print(-1)
      sys.exit()

for i in range(M):
  if (i * ts) % ss == 0:
    j = (i * ts) // ss
    if T[i] != S[j]:
      print(-1)
      sys.exit()

print(L)

Buggy python submission (verdict: Runtime Error):
import fractions


def main():
    X_S = S_ni_naru([0]*(L+1), S, L, N)
    X_T = T_ni_naru([0]*(L+1), T, L, M)

    for s, t in zip(X_S, X_T):
        if (s != t) and (s != 0) and (t != 0):
            print(-1)
            exit()

    print(L)


def S_ni_naru(X_S, S, L, N):
    for i in range(N):
        idx = L//N*i
        X_S[idx] = S[i]
    return X_S


def T_ni_naru(X_T, T, L, M):
    for i in range(M):
        idx = L//M*i
        X_T[idx] = T[i]
    return X_T


if __name__ == "__main__":
    N, M = map(int, input().split())
    S = input()
    T = input()
    L = N*M//fractions.gcd(N, M)
    main()


Fixed submission:
import sys
import fractions
N, M = map(int, input().split())
S = input()
T = input()

g = fractions.gcd(N, M)
L = N * M // g

ss = L // N
ts = L // M
for i in range(N):
  if (i * ss) % ts == 0:
    j = (i * ss) // ts
    if S[i] != T[j]:
      print(-1)
      sys.exit()

for i in range(M):
  if (i * ts) % ss == 0:
    j = (i * ts) // ss
    if T[i] != S[j]:
      print(-1)
      sys.exit()

print(L)

Buggy python submission (verdict: Runtime Error):
import fractions


def main():
    X_S = S_ni_naru([0]*L, S, L, N)
    X_T = T_ni_naru([0]*L, T, L, M)

    for s, t in zip(X_S, X_T):
        if (s != t) and (s != 0) and (t != 0):
            print(-1)
            exit()

    print(L)


def S_ni_naru(X_S, S, L, N):
    for i in range(N):
        idx = L//N*i
        X_S[idx] = S[i]
    return X_S


def T_ni_naru(X_T, T, L, M):
    for i in range(M):
        idx = L//M*i
        X_T[idx] = T[i]
    return X_T


if __name__ == "__main__":
    N, M = map(int, input().split())
    S = input()
    T = input()
    L = N*M//fractions.gcd(N, M)
    main()


Fixed submission:
import sys
import fractions
N, M = map(int, input().split())
S = input()
T = input()

g = fractions.gcd(N, M)
L = N * M // g

ss = L // N
ts = L // M
for i in range(N):
  if (i * ss) % ts == 0:
    j = (i * ss) // ts
    if S[i] != T[j]:
      print(-1)
      sys.exit()

for i in range(M):
  if (i * ts) % ss == 0:
    j = (i * ts) // ss
    if T[i] != S[j]:
      print(-1)
      sys.exit()

print(L)

Buggy python submission (verdict: Runtime Error):
import fractions


def main():
    X_S = S_ni_naru([0]*L, S, L, N)
    X_T = T_ni_naru([0]*L, T, L, M)

    for s, t in zip(X_S, X_T):
        if (s != t) and (s != 0) and (t != 0):
            print(-1)
            exit()

    print(L)


def S_ni_naru(X_S, S, L, N):
    s_i = 0
    for i in range(N):
        if i == 0:
            idx = 0
        else:
            idx = L//N*i

        X_S[idx] = S[s_i]
        s_i = s_i + 1

    return X_S


def T_ni_naru(X_T, T, L, M):
    t_i = 0
    for i in range(M):
        if i == 0:
            idx = 0
        else:
            idx = L//M*i

        X_T[idx] = T[t_i]
        t_i = t_i + 1

    return X_T


if __name__ == "__main__":
    N, M = map(int, input().split())
    S = input()
    T = input()
    L = int((N*M) / fractions.gcd(N, M))
    main()


Fixed submission:
import sys
import fractions
N, M = map(int, input().split())
S = input()
T = input()

g = fractions.gcd(N, M)
L = N * M // g

ss = L // N
ts = L // M
for i in range(N):
  if (i * ss) % ts == 0:
    j = (i * ss) // ts
    if S[i] != T[j]:
      print(-1)
      sys.exit()

for i in range(M):
  if (i * ts) % ss == 0:
    j = (i * ts) // ss
    if T[i] != S[j]:
      print(-1)
      sys.exit()

print(L)