Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #pragma comment(linker,"/STACK:1024000000,1024000000")
// 251103FD
// 4008802802
// 4008160009
// 1175114440
using namespace std;
#define PB pop_back
#define PF pop_front
#define np next_permutation
#define eb emplace_back
#define pb push_back
#define bf push_front
#define mp make_pair
#define s second
#define f first
#define foreach(it, a)                                                         \
  for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define Foreach(it, a)                                                         \
  for (__typeof((a).rbegin()) it = (a).rbegin(); it != (a).rend(); it++)
#define forn(i, a) for (register int i = 0; i < a; i++)
#define Forn(i, a) for (register int i = 1; i <= a; i++)
#define memINF 127
#define all(x) x.begin(), x.end()
#define FAST ios::sync_with_stdio(false)
#define mid ((l + r) >> 1)
#define lid (id << 1 | 1)
#define rid (id << 1) + 2
#define lson lid, l, mid
#define rson rid, mid, r

typedef long long ll;
typedef pair<int, int> pi;
typedef pair<pi, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef unsigned int uint;
typedef vector<vector<ll>> matrix;
typedef __int128 LL;

inline int in() {
  int x = 0, c;
  while ((uint)((c = getchar()) - 48) >= 10)
    if (c == '-')
      return -in();
  do {
    x = (x << 3) + (x << 1) + (c ^ 48);
  } while ((uint)((c = getchar()) ^ 48) < 10);
  return x;
}
inline int lcm(int a, int b) { return a * b / __gcd(a, b); }

const int MAX = 1e5 + 5;

ll N, M, tmp;
char s[MAX], t[MAX];

int main() {
  N = in(), M = in();
  scanf("%s%s", s, t);
  tmp = lcm(N, M);
  if (s[0] != t[0]) {
    puts("-1");
    return 0;
  }

  ll s_i = tmp / N;
  ll t_i = tmp / M;
  for (ll i = 0; i < min(N / t_i, M / s_i); i++)
    if (s[t_i * i] != t[s_i * i]) {
      puts("-1");
      return 0;
    }
  printf("%lld\n", tmp);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
// #pragma comment(linker,"/STACK:1024000000,1024000000")
// 251103FD
// 4008802802
// 4008160009
// 1175114440
using namespace std;
#define PB pop_back
#define PF pop_front
#define np next_permutation
#define eb emplace_back
#define pb push_back
#define bf push_front
#define mp make_pair
#define s second
#define f first
#define foreach(it, a)                                                         \
  for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define Foreach(it, a)                                                         \
  for (__typeof((a).rbegin()) it = (a).rbegin(); it != (a).rend(); it++)
#define forn(i, a) for (register int i = 0; i < a; i++)
#define Forn(i, a) for (register int i = 1; i <= a; i++)
#define memINF 127
#define all(x) x.begin(), x.end()
#define FAST ios::sync_with_stdio(false)
#define mid ((l + r) >> 1)
#define lid (id << 1 | 1)
#define rid (id << 1) + 2
#define lson lid, l, mid
#define rson rid, mid, r

typedef long long ll;
typedef pair<int, int> pi;
typedef pair<pi, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef unsigned int uint;
typedef vector<vector<ll>> matrix;
typedef __int128 LL;

inline int in() {
  int x = 0, c;
  while ((uint)((c = getchar()) - 48) >= 10)
    if (c == '-')
      return -in();
  do {
    x = (x << 3) + (x << 1) + (c ^ 48);
  } while ((uint)((c = getchar()) ^ 48) < 10);
  return x;
}
inline ll lcm(ll a, ll b) { return a * b / __gcd(a, b); }

const int MAX = 1e5 + 5;

ll N, M, tmp;
char s[MAX], t[MAX];

int main() {
  N = in(), M = in();
  scanf("%s%s", s, t);
  tmp = lcm(N, M);
  if (s[0] != t[0]) {
    puts("-1");
    return 0;
  }

  ll s_i = tmp / N;
  ll t_i = tmp / M;
  for (ll i = 0; i < min(N / t_i, M / s_i); i++)
    if (s[t_i * i] != t[s_i * i]) {
      puts("-1");
      return 0;
    }
  printf("%lld\n", tmp);
  return 0;
}