Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

import sys
import itertools
import math

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

    if N < M:
        t = M
        M = N
        N = t
        q = S
        S = T
        T = q

    ret = -1
    for step in range(1, M+1):
        slen = step * N
        if slen % M != 0:
            continue
        mstep = slen // M
        good = True
        for i in range(N):
            u = (step * i) % mstep
            if u != 0:
                continue
            j = (step * i) // mstep
            if S[i] != T[j]:
                good = False
                break
        if good:
            ret = step * N
            break

    # 出力
    print("{}".format(ret))


if __name__ == '__main__':
    main()



Fixed submission:
# -*- coding: utf-8 -*-

import sys
import itertools
import math

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

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

    if N < M:
        t = M
        M = N
        N = t
        q = S
        S = T
        T = q

    d = gcd(N,M)
    c = N * M // d

    ret = -1
    slen = c
    nstep = slen // N
    mstep = slen // M

    dd = gcd(nstep, mstep)
    cc = nstep * mstep // dd

    good = True
    for k in range(0, slen, cc):
        u = (k // nstep) % N
        j = (k // mstep) % M
        if S[u] != T[j]:
            good = False
            break
    if good:
        ret = slen

    # 出力
    print("{}".format(ret))


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

import sys
import itertools
import math

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

if N < M:
    t = M
    M = N
    N = t
    q = S
    S = T
    T = q

d = math.gcd(N, M)

n = N / d

ret = -1
for step in range(1, M+1):
    slen = step * N
    if slen % M != 0:
        continue
    mstep = slen // M
    good = True
    for i in range(N):
        u = (step * i) % mstep
        if u != 0:
            continue
        j = (step * i) // mstep
        if S[i] != T[j]:
            good = False
            break
    if good:
        ret = step * N
        break

# 出力
print("{}".format(ret))


Fixed submission:
# -*- coding: utf-8 -*-

import sys
import itertools
import math

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

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

    if N < M:
        t = M
        M = N
        N = t
        q = S
        S = T
        T = q

    d = gcd(N,M)
    c = N * M // d

    ret = -1
    slen = c
    nstep = slen // N
    mstep = slen // M

    dd = gcd(nstep, mstep)
    cc = nstep * mstep // dd

    good = True
    for k in range(0, slen, cc):
        u = (k // nstep) % N
        j = (k // mstep) % M
        if S[u] != T[j]:
            good = False
            break
    if good:
        ret = slen

    # 出力
    print("{}".format(ret))


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

import sys
import itertools
import math

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

    if N < M:
        t = M
        M = N
        N = t
        q = S
        S = T
        T = q

    d = math.gcd(N, M)

    n = N / d

    ret = -1
    for step in range(1, M+1):
        slen = step * N
        if slen % M != 0:
            continue
        mstep = slen // M
        good = True
        for i in range(N):
            u = (step * i) % mstep
            if u != 0:
                continue
            j = (step * i) // mstep
            if S[i] != T[j]:
                good = False
                break
        if good:
            ret = step * N
            break

    # 出力
    print("{}".format(ret))


if __name__ == '__main__':
    main()



Fixed submission:
# -*- coding: utf-8 -*-

import sys
import itertools
import math

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

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

    if N < M:
        t = M
        M = N
        N = t
        q = S
        S = T
        T = q

    d = gcd(N,M)
    c = N * M // d

    ret = -1
    slen = c
    nstep = slen // N
    mstep = slen // M

    dd = gcd(nstep, mstep)
    cc = nstep * mstep // dd

    good = True
    for k in range(0, slen, cc):
        u = (k // nstep) % N
        j = (k // mstep) % M
        if S[u] != T[j]:
            good = False
            break
    if good:
        ret = slen

    # 出力
    print("{}".format(ret))


if __name__ == '__main__':
    main()