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: Wrong Answer

Solution code:
from fractions import gcd
#from math import gcd

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

n, m = map(int, input().split())
s = input()
t = input()
L = gcd(n,m)
ans = lcm(n,m)
for i in range(L):
    if not s[L//n*i] == t[L//m*i]:
        ans = -1
        break
print(ans)

Submission status: Wrong Answer

Solution code:
import fractions

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

g = fractions.gcd(N,M)

L = g * (N // g) * (M // g)

print(-1)

Submission status: Wrong Answer

Solution code:
import fractions

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

g = fractions.gcd(N,M)

L = g * (N // g) * (M // g)

print(L)

Submission status: Wrong Answer

Solution code:
import fractions
n, m = map(int, input().split())
s = input()
t = input()
l = fractions.gcd(n, m)
a, b = n//l, m//l
for i in range(l):
    if t[b*i] != s[a*i]:
        print(-1)
        exit()
print(n*m)

Submission status: Wrong Answer

Solution code:
import collections
 
def prime_factorize(n):
  a = []
  while n % 2 == 0:
    a.append(2)
    n //= 2
  f = 3
  while f * f <= n:
    if n % f == 0:
      a.append(f)
      n //= f
    else:
      f += 2
  if n != 1:
    a.append(n)
  return a
 
N, M = map(int, input().split())
S = input()
T = input()
Ns = collections.Counter(prime_factorize(N))
Ms = collections.Counter(prime_factorize(M))
 
cfac = 1
for k in Ns:
  if Ms[k] != 0:
    cfac *= k*min(Ns[k],Ms[k])
  
B = N//cfac
C = M//cfac
 
i = 0
for i in range(cfac):
  if S[i*B] != T[i*C]:
    print(-1)
    exit()
  i += 1
  
print(cfac*B*C)

Submission status: Wrong Answer

Solution code:
import collections

def prime_factorize(n):
  a = []
  while n % 2 == 0:
    a.append(2)
    n //= 2
  f = 3
  while f * f <= n:
    if n % f == 0:
      a.append(f)
      n //= f
    else:
      f += 2
  if n != 1:
    a.append(n)
  return a

N, M = map(int, input().split())
S = input()
T = input()
Ns = collections.Counter(prime_factorize(N))
Ms = collections.Counter(prime_factorize(M))

cfac = 1
for k in Ns:
  if Ms[k] != 0:
    cfac *= k*min(Ns[k],Ms[k])
  
B = N//cfac
C = M//cfac

i = 0
while i*B < N:
  if S[i*B] != T[i*C]:
    print(-1)
    exit()
  i += 1

print(cfac*B*C)

Submission status: Wrong Answer

Solution code:
import collections

def prime_factorize(n):
  a = []
  while n % 2 == 0:
    a.append(2)
    n //= 2
  f = 3
  while f * f <= n:
    if n % f == 0:
      a.append(f)
      n //= f
    else:
      f += 2
  if n != 1:
    a.append(n)
  return a

N, M = map(int, input().split())
S = input()
T = input()
Ns = collections.Counter(prime_factorize(N))
Ms = collections.Counter(prime_factorize(M))

cfac = 1
for k in Ns:
  if Ms[k] != 0:
    cfac *= k*min(Ns[k],Ms[k])
  
C = N//cfac
B = M//cfac

i = 0
while i*C < N-C:
  if S[i*C] != T[i*B]:
    print(-1)
    exit()
  i += 1

print(cfac*B*C)

Submission status: Wrong Answer

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

import sys

if S == T:
    print(N)
    sys.exit()
print(N)

Submission status: Wrong Answer

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(N):
    X[(L // N) * i] = S[i]

for i in range(N):
    if (L // M) * i in X and X[(L // M) * i] != T[i]:
        print(-1)
        exit()
print(L)

Submission status: Wrong Answer

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 = {}

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

Submission status: Wrong Answer

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)
if ans not in (n, m):
    if s[0] == t[0]:
        print(ans)
    else:
        print(-1)
else:
    short = min(s, t, key=lambda x: len(x))
    lg = max(s, t, key=lambda x: len(x))
    d = len(lg)//len(short)
    if all(short[i] == lg[i*d] for i in range(len(short))):
        print(ans)
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
s=str(input())
s=list(s)
t=str(input())
t=list(t)
ans=-1
if s[0]==t[0]:
  import fractions
  temp=len(s)*len(t)//fractions.gcd(len(s),len(t))
  k=int(temp/len(s)*len(t))
  count=1
  for i in range(1,k):
    if s[i*int(len(s)/k)]==t[i*int(len(t)/k)]:
      count=count+1
  if count==k:
    ans=temp
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = int(input())
n, m = map(int, input().split())
s = input()
t = input()
#  = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()
#a,bの最大公約数
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#a,bの最小公倍数
def lcm(a, b):
    return a * b // gcd (a, b)

lcm_ = lcm(n, m)
if lcm_ == max(n, m):
    print(-1)
else:
    print(lcm_)

Submission status: Wrong Answer

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

l = lcm(N, M)
if l == N or l == M:
    print(-1)
else:
    print(l)

Submission status: Wrong Answer

Solution code:
from fractions 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: Wrong Answer

Solution code:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt, ceil
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)

# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

def is_prime(n):
    if n == 1:
        return False
    for i in range(2,int(n**0.5)+1):
        if n % i == 0:
            return False
    return True


# ワーシャルフロイド (任意の2頂点の対に対して最短経路を求める)
# 計算量n^3 (nは頂点の数)
def warshall_floyd(d, n):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d


# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# 素数判定



# limit以下の素数を列挙
def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P

# 同じものを含む順列
def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        # set（集合）型で重複を削除、ソート
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret


# ここから書き始める
n, m = map(int, input().split())
s = input()
t = input()
l = lcm(n, m)
yes = True
if n == m and s != t:
    yes = False
elif n > m:
    for i in range(m):
        if n / m == n // m and t[i] != s[i * (n // m)]:
            yes = False
else:
    for i in range(n):
        if m / n == m // n and s[i] != t[i * (m // n)]:
            yes = False
if yes:
    print(l)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
from fractions import gcd


N, M = map(int, input().split())
S = input()
T = input()
if N % M == 0:
    print(-1)
else:
    print(N * M // gcd(N, M))

Submission status: Wrong Answer

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 s[0] != t[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: Wrong Answer

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))
        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: Wrong Answer

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))
        aJump = sl//a
        bJump = sl//b
        for i in range(b):
            if stack%gcdAB == 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: Wrong Answer

Solution code:
from fractions import gcd

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

g = gcd(n, m)

print(-1 if s[1:g] == t[1:g] else n * m // g)

Submission status: Wrong Answer

Solution code:
from fractions import gcd

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

g = gcd(n, m)

print(-1 if s[:g] == t[:g] else n * m // g)

Submission status: Wrong Answer

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
        if a%b == 0:
            c = 1
        for i in range(b):
            if i+stack >= len(s):
                break
            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))

Submission status: Wrong Answer

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

def gcd(num1,num2):
    r=1
    while r!=0:
        r=num1%num2
        num1=num2
        num2=r
    return num1

def lcm(num1,num2):
    return num1*num2//gcd(num1,num2)

g1 = lcm(n,m)
g2 = lcm(g1//n,g1//m)

for i in range(g1//g2):
    if s[g2//g1*n*i]!=t[g2//g1*m*i]:
        print('-1')
        exit()
print(g1)

Submission status: Wrong Answer

Solution code:
from  fractions  import gcd
n,m = map(int,input().split())
s = input()
t = input()
def lcm(x,y):
  return (x*y)//gcd(x,y)

a = lcm(n,m)
l = [0 for i in range(a)]
l1 = [0 for i in range(a)]
for i in range(n):
  l[int(i*(a/n))+1-1] = s[i]
for j in range(m):
  l1[int(j*(a/m))+1-1] = t[j]
for j,k in zip(l,l1):
  if j!=k and j!=0 and k!=0:
    print("-1")
else:
  print(a)

Submission status: Wrong Answer

Solution code:
import sys
from fractions import gcd

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


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

    if N == M:
        if S == T:
            print(N)
            return
        else:
            print(-1)
            return
    elif N % M == 0:
        for i in range(M):
            if T[i] == S[L // M * i]:
                continue
            else:
                print(-1)
                return
        else:
            print(L)
    elif M % N == 0:
        for i in range(N):
            if S[i] == T[L // N * i]:
                continue
            else:
                print(-1)
                return
        else:
            print(L)
    else:
        print(L)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from fractions import gcd

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


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

    if N == M:
        if S == T:
            print(N)
            return
        else:
            print(-1)
            return
    elif N % M == 0:
        L = N
        for i in range(M):
            if T[i] == S[N // M * i]:
                continue
            else:
                print(-1)
                return
        else:
            print(L)
    elif M % N == 0:
        L = M
        for i in range(N):
            if S[i] == T[L // N * i]:
                continue
            else:
                print(-1)
                return
        else:
            print(L)
    else:
        L = M // gcd(M, N) * N
        print(L)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from fractions import gcd

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


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

    if N == M:
        if S == T:
            print(N)
            return
        else:
            print(-1)
            return
    elif N % M == 0:
        for i in range(M):
            if T[i] == S[N // M * i]:
                continue
            else:
                print(-1)
                return
        else:
            print(N)
    elif M % N == 0:
        for i in range(N):
            if T[i] == S[M // N * i]:
                continue
            else:
                print(-1)
                return
        else:
            print(M)
    else:
        L = M // gcd(M, N) * N
        print(L)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
s=str(input())
t=str(input())
def gcd(x, y):
    if x % y == 0:
        return(y)
    else:
        return gcd(y,x%y)
def lcm(a,b):
    return a*b//gcd(a,b)
if n%m!=0:
    if s[0]==t[0]:
        print(lcm(n,m))
    else:
        print("-1")
else:
    x=n//m
    for i in range(m):
        if s[0+x*i]!=t[i]:
            print("-1")
            exit()
    print(lcm(n,m))

Submission status: Wrong Answer

Solution code:
from fractions import gcd

def lcm(m, n):
    return (m * n) // gcd(m, n)

def lcm_list(a):
    x = a[0]
    for i in range(1, len(a)):
        x = (x * a[i]) // gcd(x, a[i])
    return x

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

l = lcm(N, M)

if l==N or l==M or S[0]!=T[0]:
    print(-1)
else:
    print(l)

Submission status: Wrong Answer

Solution code:
from fractions import gcd

def lcm(m, n):
    return (m * n) // gcd(m, n)

def lcm_list(a):
    x = a[0]
    for i in range(1, len(a)):
        x = (x * a[i]) // gcd(x, a[i])
    return x

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

l = lcm(N, M)

if l==N or l==M:
    print(-1)
else:
    print(l)

Submission status: Wrong Answer

Solution code:
import fractions
import bisect
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)
S_arr = [0]*N
T_arr = [0]*M
for i in range(1,N):
    S_arr[i] = i*(L//N)
for i in range(1,M):
    T_arr[i] = i*(L//M)
for i in range(min(N,M)):
    index = bisect.bisect_left(S_arr,T_arr[i])
    if S_arr[index] == T_arr[i] and S[index] != T[i]:
        print(-1)
        exit()
print(L)

Submission status: Wrong Answer

Solution code:
import sys
from fractions import gcd
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N, M = map(int, readline().split())
    S = readline().decode('utf-8')
    T = readline().decode('utf-8')
    lcm = N*M//gcd(N,M)
    if lcm==N or lcm==M:
        print(-1)
    else:
        print(lcm)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import fractions
  
def lcm(a,b):
  return (a * b) // fractions.gcd(a, b)

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

if M > N:
  N,M = M,N
  S,T = T,S

X = lcm(N,M)

n = N // fractions.gcd(N,M)
m = M // fractions.gcd(N,M)

for i in range(0,X,m):
  if S[0] != T[0]:
    print(-1)
    break
  elif i % n == 0:
    s = i // n
    t = i // m
    if S[s] != T[t]:
      print(-1)
      break
else:
  print(X)

Submission status: Wrong Answer

Solution code:
import fractions
  
def lcm(a,b):
  return (a * b) // fractions.gcd(a, b)

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

if M > N:
  N,M = M,N
  S,T = T,S

X = lcm(N,M)

n = N // fractions.gcd(N,M)
m = M // fractions.gcd(N,M)

for i in range(0,X,m):
  if i == 0:
    if S[i] != T[i]:
      print(-1)
      break
  elif i % n == 0:
    if S[n] != T[m]:
      print(-1)
      break
else:
  print(X)

Submission status: Wrong Answer

Solution code:
import fractions
  
def lcm(a,b):
  return (a * b) // fractions.gcd(a, b)

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

if M > N:
  N,M = M,N
  S,T = T,S

X = lcm(N,M)

n = N // fractions.gcd(N,M)
A = [i * n for i in range(0,N) ]
m = M // fractions.gcd(N,M)

for i in range(M):
  if i * m in A:
    a = A.index(i)
    if S[a] != T[i]:
      print(-1)
      break
else:
  print(X)

Submission status: Wrong Answer

Solution code:
import fractions
    
def lcm(a,b):
    return (a * b) // fractions.gcd(a, b)

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

if M > N:
  N,M = M,N
  S,T = T,S

LCM = lcm(N,M)

rans = [i for i in range(0,LCM,LCM // N)]
rant = [i for i in range(0,LCM,LCM // M)]

for i,j in zip(rans,rant):
  if i != j or S[i] != T[j // (N // M)]:
    print(-1)
    break
else:
  print(LCM)

Submission status: Wrong Answer

Solution code:
import fractions
    
def lcm(a,b):
    return (a * b) // fractions.gcd(a, b)

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

if M > N:
  N,M = M,N
  S,T = T,S

if N % M == 0:
  rate = N // M
  for i in range(M):
    if S[i * rate] != T[i]:
      print(-1)
      break
  else:
    print(N)
  
else:
  if S[0] == T[0]:
    print(lcm(N,M))
  else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import fractions
    
def lcm(a,b):
    return (a * b) // fractions.gcd(a, b)

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

if M > N:
  N,M = M,N
  S,T = T,S

if N % M == 0:
  rate = N // M
  for i in range(M):
    if S[i * rate] != T[i]:
      print(-1)
      break
  else:
    print(N)
  
else:
  print(lcm(N,M))

Submission status: Wrong Answer

Solution code:
def gcd(m,n):
    while (m % n != 0):
        n,m = m%n,n
    return(n)

def lcm(m,n):
    return(m * n // gcd(m,n))

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

s = input()
t = input()


g = gcd(m,n)

if(g in [n,m]):
    print(-1)
else:
    print(lcm(m,n))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
s=input()
t=input()
import fractions
x=n*m//(fractions.gcd(n,m))
if n%m==0:
  for i in range(m):
    if t[i]!=s[n*i//m]:
      print("-1")
      exit()
  print(x)
  exit()
elif m%n==0:
  for i in range(n):
    if s[i]!=t[m*i//n]:
      print("-1")
      exit()
  print(x)
  exit()
else:
  if s[0]==t[0]:
    print(x)
  else:
    print(-1)

Submission status: Wrong Answer

Solution code:
from fractions import gcd

N,M = list(map(int, input().split()))
s = input()
t = input()

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

saisho_kobaisu = (N * M) // gcd(N, M)
# print(saisho_kobaisu)

s_d = saisho_kobaisu // N
t_d = saisho_kobaisu // M

s_set = set([i * s_d + 1 for i in range(N)])
t_set = set([i * t_d + 1 for i in range(M)])

# print(s_set)
# print(t_set)
# print(s_set.intersection(t_set))
for idx in s_set.intersection(t_set):
    if idx > N or idx > M:
        break
    if s[(idx-1)//s_d] != t[(idx-1)//t_d]:
        print(-1)
        exit()

print(saisho_kobaisu)

Submission status: Wrong Answer

Solution code:
from fractions import gcd

N,M = list(map(int, input().split()))
s = input()
t = input()

saisho_kobaisu = (N * M) // gcd(N, M)
# print(saisho_kobaisu)

s_d = saisho_kobaisu // N
t_d = saisho_kobaisu // M

s_set = set([i * s_d + 1 for i in range(N)])
t_set = set([i * t_d + 1 for i in range(M)])

# print(s_set)
# print(t_set)
# print(s_set.intersection(t_set))
for idx in s_set.intersection(t_set):
    if idx > N or idx > M:
        break
    if s[(idx-1)//s_d] != t[(idx-1)//t_d]:
        print(-1)
        exit()

print(saisho_kobaisu)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
N, M = map(int, input().split())
S = input()
T = input()
GCD = gcd(N, M)
LCM = N*M // gcd(N, M)
# print(GCD, LCM)
ok = True
for i in range(GCD):
    # print(GCD//M + 1, GCD//N + 1)
    if S[i*(GCD//M)] != T[i*(GCD//N)]:
        ok = False

if ok:
    print(LCM)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


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


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


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

    L = lcm(n, m)

    if L > max(n, m):
        print(L)
    else:
        print(-1)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
from fractions import gcd
n,m = map(int, input().split())
s = input()
t = input()
# Xの長さLはnとmで割り切れる
# Xの1, L/n + 1, ..., (n-1) * L/n + 1 番目の文字をつなげるとSになる
# Xの1, L/m + 1, ..., (m-1) * L/m + 1 番目の文字をつなげるとTになる

ans = -1
N = (n*m) // gcd(n,m)
ok = True
if max(n,m) == N:
    if n > m: x,y = n,m
    else: x,y,s,t = m,n,t,s
    for i in range(y):
        # print([(N//y)*i], t[i])
        if s[(N//y)*i] != t[i]:
            ok = False
if ok: ans = N
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
import fractions
def lcm(a,b):
    f=a*b//fractions.gcd(a,b)
    return f

def main():
    n,m=map(int,input().split())
    s=input()
    t=input()
    ans=-1
    if n==m:
        if s==t:
            ans=n
    else:
        if n<m:
            t,s=s,t
            m,n=n,m
        if n%m==0:
            for i in range(m):
                if s[n//m*i]!=t[i]:
                    break
            else:
                ans=n
        elif s[0]==t[0]:
            ans=lcm(n,m)
    print(ans)
     


if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
#from itertools import combinations # (string,3) 3回
#from collections import deque
#import collections.defaultdict
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n,m = readInts()
    s = input()
    t = input()
    from fractions import gcd
    g = gcd(n,m)
    lcm = n // g * m
    if lcm > len(s) and lcm > len(t):
        print(lcm)
    else:
        print(-1)
if __name__ == '__main__':
  main()

Submission status: Wrong Answer

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


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

s1=input()

s2=input()

len_moji = lcm(n,m)

len_s1 = []

len_s2 = []

for i in range(n):
    len_s1.append(i*len_moji//n+1)

for i in range(m):
    len_s2.append(i*len_moji//m+1)
#print(len_s1,len_s2)
count = 0
for ix, vs1 in enumerate(len_s1):
    for jx in range(count,len(len_s2)):
        if len_s2[jx] < vs1:
            break
        if vs1 == len_s2[jx]:
            count = jx
            if s1[ix] != s2[jx]:
                print(-1)
                exit()
print(len_moji)

Submission status: Wrong Answer

Solution code:
import fractions
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import string
import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


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

        lcm = N * M // fractions.gcd(N, M)

        lcm2 = (lcm // N) * (lcm // M) // fractions.gcd((lcm // N), (lcm // M))
        i = 0
        while True:
            while lcm2 * i < min(N, M):
                if S[lcm2 * i // (lcm // N)] != T[lcm2 * i // (lcm // M)]:
                    return -1
                i += 1
            else:
                return lcm

    print(main())


resolve()

Submission status: Wrong Answer

Solution code:
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
L=N*M//gcd(N,M)
ans=L


U=[]
V=[]

#1個目
for i in range(N):
  U.append( [i*(L//N),S[i]])
#2個目
for i in range(M):
  V.append( [i*(L//M),T[i]])
#print(U)
#print(V)
for i in U:
  for j in V:
    if i[0]==j[0] and i[0]!=j[1]:
      print(-1)
      exit()


#  U[i*(L//N)]=S[i]
#  if U[j*(L//M)]!="#" and U[j*(L//M)]!=T[j]:
print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
N,M=map(int,input().split())
S=input()
T=input()
ans=-1
L=N*M//gcd(N,M)

U=[]
V=[]

#1個目
for i in range(N):
  U.append( [i*(L//N),S[i]])
#2個目
for i in range(M):
  V.append( [i*(L//M),T[i]])
print(U)
print(V)


#  U[i*(L//N)]=S[i]
#  if U[j*(L//M)]!="#" and U[j*(L//M)]!=T[j]:
print(ans)

Submission status: Wrong Answer

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

L=N*M//gcd(N,M)
#print(L)
U=[1 for _ in range(L)]
ans=L

#for i in range(N):
#  U[i*(L//N)]=S[i]
#print(U)

#for j in range(M):
#  if U[j*(L//M)]!="#" and U[j*(L//M)]!=T[j]:
#    ans=-1
#    break
#  else:
#    U[j*(L//M)]=T[j]

#print(U)
print(ans)

Submission status: Wrong Answer

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

L=N*M//gcd(N,M)
#print(L)
#U=["#" for _ in range(L)]
ans=L

#for i in range(N):
#  U[i*(L//N)]=S[i]
#print(U)

#for j in range(M):
#  if U[j*(L//M)]!="#" and U[j*(L//M)]!=T[j]:
#    ans=-1
#    break
#  else:
#    U[j*(L//M)]=T[j]

#print(U)
print(ans)

Submission status: Wrong Answer

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

L=N*M//gcd(N,M)
#print(L)
U=["#" for _ in range(L)]
ans=L

#for i in range(N):
#  U[i*(L//N)]=S[i]
#print(U)

#for j in range(M):
#  if U[j*(L//M)]!="#" and U[j*(L//M)]!=T[j]:
#    ans=-1
#    break
#  else:
#    U[j*(L//M)]=T[j]

#print(U)
print(ans)