Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

// マクロ&定数 ============================================================
typedef unsigned int uint;
typedef long long ll;
// typedef pair<int, int> P;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<double> vdouble;
typedef vector<bool> vbool;

typedef vector<vector<int>> vvint;
typedef vector<vector<ll>> vvll;
typedef vector<vector<double>> vvdouble;
typedef vector<vector<bool>> vvbool;

const int INF = 1e9 + 1;
const ll LLINF = 1e17 + 1;
const int DX[9] = {0, 0, 1, -1, 1, 1, -1, -1, 0}; // 4;４近傍
const int DY[9] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; // 8:８近傍 9:(0,0)を含む
const ll MOD = 1e9 + 7;                           // 10^9 + 7
const double PI = 3.14159265358979323846264338327950288;
//========================================================================

ll gcd(ll a, ll b);

int main() {

  //==================================
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(30);
  //==================================

  ll N, M;
  cin >> N >> M;

  string S, T;
  cin >> S >> T;

  if (S[0] != T[0]) {
    cout << -1;
    return 0;
  }

  ll L = gcd(M, N);
  L *= (N / L) * (M / L);
  for (ll i = 0; i < L; i++) {

    if (i % (L / N) == 0 && i % (L / M) == 0 &&
        S[i / (L / N)] != T[i / (L / M)]) {
      cout << -1;
      return 0;
    }
  }

  cout << L;
}

ll gcd(ll a, ll b) {
  if (a < b) {
    ll tmp = a;
    a = b;
    b = tmp;
  }

  ll r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }

  return b;
}

Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

// マクロ&定数 ============================================================
typedef unsigned int uint;
typedef long long ll;
// typedef pair<int, int> P;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<double> vdouble;
typedef vector<bool> vbool;

typedef vector<vector<int>> vvint;
typedef vector<vector<ll>> vvll;
typedef vector<vector<double>> vvdouble;
typedef vector<vector<bool>> vvbool;

const int INF = 1e9 + 1;
const ll LLINF = 1e17 + 1;
const int DX[9] = {0, 0, 1, -1, 1, 1, -1, -1, 0}; // 4;４近傍
const int DY[9] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; // 8:８近傍 9:(0,0)を含む
const ll MOD = 1e9 + 7;                           // 10^9 + 7
const double PI = 3.14159265358979323846264338327950288;
//========================================================================

ll gcd(ll a, ll b);

int main() {

  //==================================
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(30);
  //==================================

  ll N, M;
  cin >> N >> M;

  string S, T;
  cin >> S >> T;

  if (S[0] != T[0]) {
    cout << -1;
    return 0;
  }

  ll L = gcd(M, N);
  L *= (N / L) * (M / L);
  for (ll i = 0; i < L; i += L / min(N, M)) {

    if (i % (L / N) == 0 && i % (L / M) == 0 &&
        S[i / (L / N)] != T[i / (L / M)]) {
      cout << -1;
      return 0;
    }
  }

  cout << L;
}

ll gcd(ll a, ll b) {
  if (a < b) {
    ll tmp = a;
    a = b;
    b = tmp;
  }

  ll r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }

  return b;
}