Problem p02761 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define int long long
#define double long double
#define fo(a, b) for (int a = 0; a < b; a++)
#define Sort(a) sort(a.begin(), a.end())
#define rev(a) reverse(a.begin(), a.end())
#define fi first
#define se second
#define sz size()
#define bgn begin()
#define en end()
#define pb push_back
#define pp() pop_back()
#define V vector
#define P pair
#define yuko(a) setprecision(a)
#define uni(a) a.erase(unique(a.begin(), a.end()), a.end())
#define Q queue
#define pri priority_queue
#define Pri priority_queue<int, vector<int>, greater<int>>
#define PriP                                                                   \
  priority_queue<P<int, int>, vector<P<int, int>>, greater<P<int, int>>>
#define all(a) (a).begin(), (a).end()

int keta(int a) {
  double b = a;
  b = log10(b);
  int c = b;
  return c + 1;
}
signed main() {
  int n, m;
  cin >> n >> m;
  V<P<int, int>> a(m);
  fo(i, m) { cin >> a[i].fi >> a[i].se; }
  Sort(a);
  int b = 0;
  fo(i, m - 1) {
    if (a[i].fi == a[i + 1].fi && a[i].se != a[i].se)
      b = 1;
  }
  if (b) {
    cout << -1 << endl;
    return 0;
  }
  uni(a);
  if (n == 1 && a[0].se == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (n == 2 || n == 3) {
    fo(i, m) {
      if (a[i].fi == 1 && a[i].se == 0) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  for (int i = 1; i <= 999; i++) {
    if (keta(i) != n)
      continue;
    int d = 1;
    string c = to_string(i);
    fo(j, m) {
      if (c[a[j].fi - 1] - '0' != a[j].se) {
        d = 0;
      }
    }
    if (d) {
      cout << i << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define int long long
#define double long double
#define fo(a, b) for (int a = 0; a < b; a++)
#define Sort(a) sort(a.begin(), a.end())
#define rev(a) reverse(a.begin(), a.end())
#define fi first
#define se second
#define sz size()
#define bgn begin()
#define en end()
#define pb push_back
#define pp() pop_back()
#define V vector
#define P pair
#define yuko(a) setprecision(a)
#define uni(a) a.erase(unique(a.begin(), a.end()), a.end())
#define Q queue
#define pri priority_queue
#define Pri priority_queue<int, vector<int>, greater<int>>
#define PriP                                                                   \
  priority_queue<P<int, int>, vector<P<int, int>>, greater<P<int, int>>>
#define all(a) (a).begin(), (a).end()

int keta(int a) {
  double b = a;
  b = log10(b);
  int c = b;
  return c + 1;
}
signed main() {
  int n, m;
  cin >> n >> m;
  V<P<int, int>> a(m);
  fo(i, m) { cin >> a[i].fi >> a[i].se; }
  Sort(a);
  int b = 0;
  fo(i, m - 1) {
    if (a[i].fi == a[i + 1].fi && a[i].se != a[i].se)
      b = 1;
  }
  if (b) {
    cout << -1 << endl;
    return 0;
  }
  uni(a);
  if (n == 1 && m == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (n == 2 && m == 0) {
    cout << 10 << endl;
    return 0;
  }
  if (n == 3 && m == 0) {
    cout << 100 << endl;
    return 0;
  }
  if (n == 1 && a[0].se == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (n == 2 || n == 3) {
    fo(i, m) {
      if (a[i].fi == 1 && a[i].se == 0) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  for (int i = 1; i <= 999; i++) {
    if (keta(i) != n)
      continue;
    int d = 1;
    string c = to_string(i);
    fo(j, m) {
      if (c[a[j].fi - 1] - '0' != a[j].se) {
        d = 0;
      }
    }
    if (d) {
      cout << i << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}