Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <random>
//-------------------------------------------------------------------------
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define prv(v)                                                                 \
  for (auto i : v) {                                                           \
    cout << i << " ";                                                          \
  }
#define prmat(v)                                                               \
  for (auto i : v) {                                                           \
    prv(i);                                                                    \
    cout << "\n";                                                              \
  }
#define cout(x) cout << fixed << setprecision(x)
#define read(v)                                                                \
  for (int i_ = 0; i_ < v.size(); i_++) {                                      \
    cin >> v[i_];                                                              \
  }
#define fast_io ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define all(v) (v.begin()), (v.end())
#define rall(v) (v.rbegin()), (v.rend())
#define pii pair<ll, ll>
//------------------------------------------------------------------------
ll gcd(ll a, ll b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
int main() {
  fast_io;
#ifdef LOCALKA
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ll n, m;
  string s, t;
  cin >> n >> m >> s >> t;
  ll len = (n * m) / gcd(n, m);
  if (len == max(n, m)) {
    len = -1;
  }
  for (int i = 0; i < n; i++) {
    ll cur_pos = i * (len / n) + 1;
    ll j = -1;
    if ((cur_pos - 1) % (len / m) == 0) {
      j = (cur_pos - 1) / (len / m);
    }
    if (j != -1 && s[i] != t[j]) {
      cout << -1;
      return 0;
    }
  }
  cout << len;
}


Fixed submission:
#include <bits/stdc++.h>
#include <random>
//-------------------------------------------------------------------------
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define prv(v)                                                                 \
  for (auto i : v) {                                                           \
    cout << i << " ";                                                          \
  }
#define prmat(v)                                                               \
  for (auto i : v) {                                                           \
    prv(i);                                                                    \
    cout << "\n";                                                              \
  }
#define cout(x) cout << fixed << setprecision(x)
#define read(v)                                                                \
  for (int i_ = 0; i_ < v.size(); i_++) {                                      \
    cin >> v[i_];                                                              \
  }
#define fast_io ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define all(v) (v.begin()), (v.end())
#define rall(v) (v.rbegin()), (v.rend())
#define pii pair<ll, ll>
//------------------------------------------------------------------------
ll gcd(ll a, ll b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
int main() {
  fast_io;
#ifdef LOCALKA
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ll n, m;
  string s, t;
  cin >> n >> m >> s >> t;
  ll len = (n * m) / gcd(n, m);
  if (len == max(n, m)) {
    cout << -1;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    ll cur_pos = i * (len / n) + 1;
    ll j = -1;
    if ((cur_pos - 1) % (len / m) == 0) {
      j = (cur_pos - 1) / (len / m);
    }
    if (j != -1 && s[i] != t[j]) {
      cout << -1;
      return 0;
    }
  }
  cout << len;
}