Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <utility>
#include <vector>

#define ALL(obj) (obj).begin(), (obj).end()
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); (b) <= i; i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n; n <= i; i--)
#define ABS(a) ((a < 0) ? ((-1) * (a)) : (a))
#define elif else if
#define MOD 1000000007
#define INF (1 << 29)

using namespace std;

#define ld long double
#define ll long long

map<int, int> mpa, mpb;
typedef pair<ll, ll> P;
priority_queue<P, vector<P>, greater<P>> pque;

ll gcd(ll m, ll n) {
  // 引数に０がある場合は０を返す
  if ((0 == m) || (0 == n))
    return 0;

  // ユークリッドの方法
  while (m != n) {
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
} // gcd

ll lcm(ll m, ll n) {
  // 引数に０がある場合は０を返す
  if ((0 == m) || (0 == n))
    return 0;
  return ((m / gcd(m, n)) * n); // lcm = m * n / gcd(m,n)
} // lcm

// a x + b y = gcd(a, b)
int extgcd(int a, int b, int &x, int &y) {
  int g = a;
  x = 1;
  y = 0;
  if (b != 0)
    g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}
int invMod(int a, int m) {
  int x, y;
  if (extgcd(a, m, x, y) == 1)
    return (x + m) % m;
  else
    return 0; // unsolvable
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  ll N, M;
  cin >> N >> M;
  string S, T;
  ll ans = lcm(N, M);
  cin >> S >> T;
  for (int i = 0; i < N * M; i++) {
    if (S[i * ans / M] != T[i * ans / N]) {
      cout << -1 << endl;
      return 0;
    }
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <utility>
#include <vector>

#define ALL(obj) (obj).begin(), (obj).end()
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); (b) <= i; i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n; n <= i; i--)
#define ABS(a) ((a < 0) ? ((-1) * (a)) : (a))
#define elif else if
#define MOD 1000000007
#define INF (1 << 29)

using namespace std;

#define ld long double
#define ll long long

map<int, int> mpa, mpb;
typedef pair<ll, ll> P;
priority_queue<P, vector<P>, greater<P>> pque;

ll gcd(ll m, ll n) {
  // 引数に０がある場合は０を返す
  if ((0 == m) || (0 == n))
    return 0;

  // ユークリッドの方法
  while (m != n) {
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
} // gcd

ll lcm(ll m, ll n) {
  // 引数に０がある場合は０を返す
  if ((0 == m) || (0 == n))
    return 0;
  return ((m / gcd(m, n)) * n); // lcm = m * n / gcd(m,n)
} // lcm

// a x + b y = gcd(a, b)
int extgcd(int a, int b, int &x, int &y) {
  int g = a;
  x = 1;
  y = 0;
  if (b != 0)
    g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}
int invMod(int a, int m) {
  int x, y;
  if (extgcd(a, m, x, y) == 1)
    return (x + m) % m;
  else
    return 0; // unsolvable
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  ll N, M;
  cin >> N >> M;
  string S, T;
  ll ans = lcm(N, M);
  cin >> S >> T;
  for (int i = 0; i < gcd(N, M); i++) {
    if (S[i * ans / M] != T[i * ans / N]) {
      cout << -1 << endl;
      return 0;
    }
  }

  cout << ans << endl;
  return 0;
}