Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
typedef long long int ll;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < signed(n); i++)
#define EREP(i, n) for (int i = 1; i <= signed(n); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
// #define EVEL 1
#ifdef EVEL
#define DEB(X) cout << #X << ":" << X << " ";
#define TF(f) f ? cout << "true  " : cout << "false ";
#define END cout << "\n";
#else
#define DEB(X)                                                                 \
  {}
#define TF(f)                                                                  \
  {}
#define END                                                                    \
  {}
#endif
constexpr int MOD = 1000000007;
constexpr ll INF = 50000000000000000;
typedef std::pair<int, int> P;
constexpr ll SIZE = 100010;

ll N, M;
ll ans = 0;
string S, T;
bool F = true;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int main() {
  std::ios_base::sync_with_stdio(false);
  cin >> N >> M >> S >> T;
  ll G = gcd(N, M);
  ll L = N / G * M;
  ll Q = (L / M) / gcd(L / M, L / N) * (L / N);
  ll loop = min(N, M);
  ans = L;
  DEB(loop) END for (int i = 1; i <= loop; i += Q) {
    DEB(i) DEB(S[i]) DEB(T[i]) END if (S[i - 1] != T[i - 1]) {
      F = false;
      break;
    }
  }
  cout << (F ? ans : -1) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
typedef long long int ll;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < signed(n); i++)
#define EREP(i, n) for (int i = 1; i <= signed(n); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
// #define EVEL 1
#ifdef EVEL
#define DEB(X) cout << #X << ":" << X << " ";
#define TF(f) f ? cout << "true  " : cout << "false ";
#define END cout << "\n";
#else
#define DEB(X)                                                                 \
  {}
#define TF(f)                                                                  \
  {}
#define END                                                                    \
  {}
#endif
constexpr int MOD = 1000000007;
constexpr ll INF = 50000000000000000;
typedef std::pair<int, int> P;
constexpr ll SIZE = 100010;

ll N, M;
ll ans = 0;
string S, T;
bool F = true;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int main() {
  std::ios_base::sync_with_stdio(false);
  cin >> N >> M >> S >> T;
  ll G = gcd(N, M);
  ll L = N / G * M;
  ll Q = (L / M) / gcd(L / M, L / N) * (L / N);
  ll loop = min(N, M);
  ans = L;
  DEB(loop)
  DEB(Q) END for (int i = 0, j = 0; j < T.size() && i < S.size();
                  i += Q / (L / N), j += Q / (L / M)) {
    DEB(i) DEB(j) DEB(S[i]) DEB(T[j]) END if (S[i] != T[j]) {
      F = false;
      break;
    }
  }
  cout << (F ? ans : -1) << endl;
  return 0;
}