Problem p03231 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cmath>

// using namespace boost::multiprecision;
using namespace std;
typedef long long ll;
// typedef unsigned long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
// #define rep(i, n) for (ll i = 0; i < (n); ++i)
// #define sz(x) ll(x.size())
typedef pair<int, int> P;
// typedef pair<ll, int> P;
// typedef pair<ll, ll> P;
// const double INF = 1e10;
const ll INF = LONG_LONG_MAX / 100;
// const ll INF = (1ll << 31) - 1;
// const ll INF = 1e15;
const ll MINF = LONG_LONG_MIN;
// const int INF = INT_MAX / 10;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
// typedef pair<int, int> P;
// typedef pair<double, double> P;
#define ret() return 0;

bool contain(set<char> &s, char a) { return s.find(a) != s.end(); }

// ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
// ofstream outfile("log.txt");
// outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0')
// << rank << endl;
//  std::cout << std::bitset<8>(9);
const int mod = 1000000007;
// const ll mod = 1e10;
typedef priority_queue<long long, vector<long long>, greater<long long>> PQ_ASK;

ll gcd(ll x, ll y) {
  if (x > y)
    swap(x, y);
  ll m = 1;
  while (m != 0) {
    m = y % x;
    y = x;
    x = m;
  }
  return y;
}

ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

int main() {
  int n, m;
  string s, t;
  cin >> n >> m >> s >> t;

  ll l = lcm(n, m);
  vector<char> x(l, ' ');

  for (int i = 0; i < n; i++) {
    x[l / n * i] = s[i];
  }
  for (int i = 0; i < m; i++) {
    x[l / m * i] = t[i];
  }
  for (int i = 0; i < n; i++) {
    if (x[l / n * i] != s[i]) {
      cout << -1 << endl;
      ret();
    }
  }
  cout << l << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <cmath>

// using namespace boost::multiprecision;
using namespace std;
typedef long long ll;
// typedef unsigned long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
// #define rep(i, n) for (ll i = 0; i < (n); ++i)
// #define sz(x) ll(x.size())
typedef pair<int, int> P;
// typedef pair<ll, int> P;
// typedef pair<ll, ll> P;
// const double INF = 1e10;
const ll INF = LONG_LONG_MAX / 100;
// const ll INF = (1ll << 31) - 1;
// const ll INF = 1e15;
const ll MINF = LONG_LONG_MIN;
// const int INF = INT_MAX / 10;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
// typedef pair<int, int> P;
// typedef pair<double, double> P;
#define ret() return 0;

bool contain(set<char> &s, char a) { return s.find(a) != s.end(); }

// ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
// ofstream outfile("log.txt");
// outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0')
// << rank << endl;
//  std::cout << std::bitset<8>(9);
const int mod = 1000000007;
// const ll mod = 1e10;
typedef priority_queue<long long, vector<long long>, greater<long long>> PQ_ASK;

ll gcd(ll x, ll y) {
  if (x > y)
    swap(x, y);
  ll m = 1;
  while (m != 0) {
    m = y % x;
    y = x;
    x = m;
  }
  return y;
}

ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

int main() {
  int n, m;
  string s, t;
  cin >> n >> m >> s >> t;

  ll l = lcm(n, m);
  map<int, char> x;

  for (int i = 0; i < n; i++) {
    x[l / n * i] = s[i];
  }
  for (int i = 0; i < m; i++) {
    x[l / m * i] = t[i];
  }
  for (int i = 0; i < n; i++) {
    if (x[l / n * i] != s[i]) {
      cout << -1 << endl;
      ret();
    }
  }
  cout << l << endl;
}