Problem p03506 (C++)

Buggy submission (status: Runtime Error):
/*I MAY NOT GET THE SUCCESS IMMEDIATELY BUT I WILL GET IT FOR SURE*/
#include <bits/stdc++.h>
#define opt std::ios_base::sync_with_stdio(false)
#define I int
#define li int32_t
#define lli long long
#define ulli unsigned long long

#define pn printf("\n")
#define nl cout << '\n'
#define sf(N) scanf("%lld", &N)
#define pf(N) printf("%lld", N)
#define sf1(N1, N2) scanf("%lld %lld", &N1, &N2)
#define pf1(N1, N2) printf("%lld %lld", N1, N2)
#define sf2(N1, N2, N3) scanf("%lld %lld %lld", &N1, &N2, &N3)
#define pf2(N1, N2, N3) printf("%lld %lld %lld", N1, N2, N3)

#define sl cout << ' '
#define ps printf(" ")

#define rep(i, a, b) for (i = a; i < b; i++)
#define repr(i, a, b) for (i = a; i > b; i--)
#define elif else if
#define mset(a, b) memset(a, b, sizeof(a))

#define pb push_back
#define pob pop_back
#define itr iterator
#define sz() size()
#define szof sizeof
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define vlli vector<lli>
#define plli pair<lli, lli>
#define vplli vector<plli>
#define F first
#define S second
#define Dup erase(unique(V.begin(), V.end()), V.end())

#define bs binary_search
#define ub upper_bound
#define lb lower_bound

#define ALL(V) V.begin(), V.end()

#define MX1 100000LL
#define MX2 1000000LL
#define Inf 1000000000000000LL
#define mod 1000000007LL
using namespace std;

lli Power(lli a, lli b) {
  lli result = 1;

  while (b) {
    if (b % 2) {
      result = (result * a) % mod;
    }

    b = b >> 1;
    a = (a * a) % mod;
  }

  return result;
}

int main() {
  // opt;

  /*#ifndef ONLINE_JUDGE
  freopen("double_squares.txt","r",stdin);
  freopen("double_squares_out.txt","w",stdout);
  #endif // ONLINE_JUDGE*/

  int N, i, Q;
  scanf("%d %d", &N, &Q);

  while (Q--) {
    int a, b;
    scanf("%d %d", &a, &b);

    assert(N != 1);

    --a, --b;

    while (a != b) {
      if (a > b) {
        a = (a - 1) / N;
      } else {
        b = (b - 1) / N;
      }
    }

    printf("%d\n", a + 1);
  }

  return 0;
}

Fixed submission:
/*I MAY NOT GET THE SUCCESS IMMEDIATELY BUT I WILL GET IT FOR SURE*/
#include <bits/stdc++.h>
#define opt std::ios_base::sync_with_stdio(false)
#define I int
#define li int32_t
#define lli long long
#define ulli unsigned long long

#define pn printf("\n")
#define nl cout << '\n'
#define sf(N) scanf("%lld", &N)
#define pf(N) printf("%lld", N)
#define sf1(N1, N2) scanf("%lld %lld", &N1, &N2)
#define pf1(N1, N2) printf("%lld %lld", N1, N2)
#define sf2(N1, N2, N3) scanf("%lld %lld %lld", &N1, &N2, &N3)
#define pf2(N1, N2, N3) printf("%lld %lld %lld", N1, N2, N3)

#define sl cout << ' '
#define ps printf(" ")

#define rep(i, a, b) for (i = a; i < b; i++)
#define repr(i, a, b) for (i = a; i > b; i--)
#define elif else if
#define mset(a, b) memset(a, b, sizeof(a))

#define pb push_back
#define pob pop_back
#define itr iterator
#define sz() size()
#define szof sizeof
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define vlli vector<lli>
#define plli pair<lli, lli>
#define vplli vector<plli>
#define F first
#define S second
#define Dup erase(unique(V.begin(), V.end()), V.end())

#define bs binary_search
#define ub upper_bound
#define lb lower_bound

#define ALL(V) V.begin(), V.end()

#define MX1 100000LL
#define MX2 1000000LL
#define Inf 1000000000000000LL
#define mod 1000000007LL
using namespace std;

lli Power(lli a, lli b) {
  lli result = 1;

  while (b) {
    if (b % 2) {
      result = (result * a) % mod;
    }

    b = b >> 1;
    a = (a * a) % mod;
  }

  return result;
}

int main() {
  // opt;

  /*#ifndef ONLINE_JUDGE
  freopen("double_squares.txt","r",stdin);
  freopen("double_squares_out.txt","w",stdout);
  #endif // ONLINE_JUDGE*/

  int N, i, Q;
  scanf("%d %d", &N, &Q);

  while (Q--) {
    int a, b;
    scanf("%d %d", &a, &b);

    if (N == 1) {
      printf("%d\n", min(a, b));
      continue;
    }

    --a, --b;

    while (a != b) {
      if (a > b) {
        a = (a - 1) / N;
      } else {
        b = (b - 1) / N;
      }
    }

    printf("%d\n", a + 1);
  }

  return 0;
}