output_description: If a good string does not exist, print `-1`; if it exists, print the length of
  the shortest such string.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N M
      S
      T
attempt: # https://atcoder.jp/contests/agc028/tasks/agc028_a
  
  # 問題からエスパーするとlcmが答えになる
  # lcmで作れないならそれ以上伸ばしても絶対作れない
  import sys
  
  sys.setrecursionlimit(1 << 25)
  read = sys.stdin.readline
  ra = range
  enu = enumerate
  
  
  def mina(*argv, sub=1):
      return list(map(lambda x: x - sub, argv))
  
  
  # 受け渡されたすべての要素からsubだけ引く.リストを*をつけて展開しておくこと
  
  
  def read_ints():
      return list(map(int, read().split()))
  
  
  from math import gcd
  
  
  def lcm(a, b):
      # 最小公倍数
      g = gcd(a, b)
      return a // g * b
  
  
  N, M = read_ints()
  S = read()[:-1]
  T = read()[:-1]
  
  L = lcm(N, M)
  # 衝突する文字は各文字数をgcdで割った文字ごとに出現する
  s_step = N // gcd(N, M)
  t_step = M // gcd(N, M)
  print(L if S[::s_step] == T[::t_step] else -1)
  
problem_description: 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.
samples: [{"input": "3 2\n    acp\n    ae", "output": "6\n    \n\nFor example, the string `accept` is a good string. There is no good string\nshorter than this, so the answer is 6.\n\n* * *"}, {"input": "6 3\n    abcdef\n    abc", "output": "-1\n    \n\n* * *"}, {"input": "15 9\n    dnsusrayukuaiia\n    dujrunuma", "output": "45"}]