Problem p03231 (python)

Fixed submission:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from math import gcd
def main():
    n, m = map(int, input().split())
    s = tuple(input())
    t = tuple(input())

    if n > m:
        l1 = n
        l2 = m
        s1 = s
        s2 = t
    else:
        l1 = m
        l2 = n
        s1 = t
        s2 = s

    if s[0] != t[0]:
        r = -1
    elif n == m:
        if s == t:
            r = n
        else:
            r = -1
    elif n % m == 0 or m % n == 0:
        ld = l1 // l2
        for i1 in range(1, l2):
            if s2[i1] != s1[i1*ld]:
                r = -1
                break
        else:
            r = l1
    else:
        l = (n * m) // gcd(n, m)
        # x = [0] * l
        ld1 = l // l1
        ld2 = l // l2
        cnt = 1
        r = l
        while cnt < l1:
            cnt2 = cnt * ld1
            if cnt2 % ld2 == 0:
                if s1[cnt] != s2[cnt2 // ld2]:
                    r = -1
                    break
            cnt += 1
    print(r)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from math import gcd
def main():
    n, m = map(int, input().split())
    s = tuple(input())
    t = tuple(input())

    if s[0] != t[0]:
        print(-1)
        sys.exit()
    if n == m:
        if s == t:
            print(n)
            sys.exit()
        else:
            print(-1)
            sys.exit()
    if n > m:
        l1 = n
        l2 = m
        s1 = s
        s2 = t
    else:
        l1 = m
        l2 = n
        s1 = t
        s2 = s
    if n % m == 0 or m % n == 0:
        ld = l1 // l2
        for i1 in range(1, l2):
            if s2[i1] != s1[i1*ld]:
                print(-1)
                sys.exit()
        print(l1)
        sys.exit()

    l = (n * m) // gcd(n, m)
    x = [0] * l
    ld1 = l // l1
    ld2 = l // l2
    for j1 in range(1, l1):
        x[j1 * ld1] = s1[j1]
    for k1 in range(1, l2):
        if x[k1 * ld2]:
            if x[k1 * ld2] != s2[k1]:
                print(-1)
                sys.exit()
    print(l)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from math import gcd
def main():
    n, m = map(int, input().split())
    s = list(input())
    t = list(input())

    if s[0] != t[0]:
        print(-1)
        sys.exit()
    if n == m:
        if s == t:
            print(n)
            sys.exit()
        else:
            print(-1)
            sys.exit()
    if n > m:
        l1 = n
        l2 = m
        s1 = s
        s2 = t
    else:
        l1 = m
        l2 = n
        s1 = t
        s2 = s
    if n % m == 0 or m % n == 0:
        ld = l1 // l2
        for i1 in range(1, l2):
            if s2[i1] != s1[i1*ld]:
                print(-1)
                sys.exit()
        print(l1)
        sys.exit()

    l = (n * m) // gcd(n, m)
    x = [0] * l
    ld1 = l // l1
    ld2 = l // l2
    for j1 in range(1, l1):
        x[j1 * ld1] = s1[j1]
    for k1 in range(1, l2):
        if x[k1 * ld2]:
            if x[k1 * ld2] != s2[k1]:
                print(-1)
                sys.exit()
    print(l)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from math import gcd
def main():
    n, m = map(int, input().split())
    s = list(input())
    t = list(input())

    if s[0] != t[0]:
        print(-1)
        sys.exit()
    if n == m:
        if s == t:
            print(n)
        else:
            print(-1)
        sys.exit()
    if n > m:
        l1 = n
        l2 = m
        s1 = s
        s2 = t
    else:
        l1 = m
        l2 = n
        s1 = t
        s2 = s
    if n % m == 0 or m % n == 0:
        ld = l1 // l2
        for i1 in range(1, l2):
            if s2[i1] != s1[i1*ld]:
                print(-1)
                sys.exit()
        print(l1)
    else:
        l = (n * m) // gcd(n, m)
        x = [0] * l
        ld1 = l // l1
        ld2 = l // l2
        for j1 in range(1, l1):
            x[j1 * ld1] = s1[j1]
        for k1 in range(1, l2):
            if x[k1 * ld2]:
                if x[k1 * ld2] != s2[k1]:
                    print(-1)
                    sys.exit()
        print(l)

if __name__ == '__main__':
    main()