Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define mp make_pair
#define pb push_back
#define repi(i, p, q) for (long i = p; i < q; i++)
#define repd(i, p, q) for (long i = p; i >= q; i--)
#define sz(x) x.size()
#define INF 2000000000
#define MOD 1000000007

typedef map<int, int> MI;
typedef pair<int, int> PI;
typedef long long int LLI;
typedef long int LI;
typedef int I;

LLI first[100003], second[100003];

LLI gcd(LLI a, LLI b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

LLI lcm(LLI a, LLI b) { return (a * b) / gcd(a, b); }

int main() {
  LLI n, m;
  cin >> n >> m;
  LLI o = lcm(n, m);
  string s, t;
  cin >> s >> t;
  first[0] = 1;
  second[0] = 1;
  for (LLI i = 1; i < n; i++) {
    first[i] = i * (o / n) + 1;
    // cout << first[i] << " ";
  }
  // cout << "\n";
  for (LLI i = 1; i < m; i++) {
    second[i] = i * (o / m) + 1;
    // cout << second[i] << " ";
  }
  // cout << "\n";
  int flag = 0;
  LLI i = 0, j = 0;
  while (1) {
    if (i >= n && j >= m) {
      break;
    }
    if (first[i] == second[j]) {
      // cout << first[i]-1 << " " << second[j]-1 << " " << s[i] << " " << t[j]
      // << "\n";
      if (s[i] != t[j]) {
        flag = 1;
      }
      i++;
      j++;
    } else if (first[i] < second[j]) {
      i++;
    } else {
      j++;
    }
  }
  if (flag == 0) {
    cout << o;
  } else {
    cout << -1;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define mp make_pair
#define pb push_back
#define repi(i, p, q) for (long i = p; i < q; i++)
#define repd(i, p, q) for (long i = p; i >= q; i--)
#define sz(x) x.size()
#define INF 2000000000
#define MOD 1000000007

typedef map<int, int> MI;
typedef pair<int, int> PI;
typedef long long int LLI;
typedef long int LI;
typedef int I;

LLI first[100003], second[100003];

LLI gcd(LLI a, LLI b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

LLI lcm(LLI a, LLI b) { return (a * b) / gcd(a, b); }

int main() {
  LLI n, m;
  cin >> n >> m;
  LLI o = lcm(n, m);
  string s, t;
  cin >> s >> t;
  first[0] = 1;
  second[0] = 1;
  for (LLI i = 1; i < n; i++) {
    first[i] = i * (o / n) + 1;
    // cout << first[i] << " ";
  }
  // cout << "\n";
  for (LLI i = 1; i < m; i++) {
    second[i] = i * (o / m) + 1;
    // cout << second[i] << " ";
  }
  // cout << "\n";
  int flag = 0;
  LLI i = 0, j = 0;
  while (1) {
    if (i >= n || j >= m) {
      break;
    }
    if (first[i] == second[j]) {
      // cout << first[i]-1 << " " << second[j]-1 << " " << s[i] << " " << t[j]
      // << "\n";
      if (s[i] != t[j]) {
        flag = 1;
      }
      i++;
      j++;
    } else if (first[i] < second[j]) {
      i++;
    } else {
      j++;
    }
  }
  if (flag == 0) {
    cout << o;
  } else {
    cout << -1;
  }
  return 0;
}