#include <bits/stdc++.h>
using namespace std;
const int N = 4;
int n, m;
int s[N], c[N];
int dig[N] = {-1, -1, -1, -1};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> s[i] >> c[i];
    if (dig[s[i]] == -1)
      dig[s[i]] = c[i];
    else if (dig[s[i]] != c[i]) {
      cout << -1 << '\n';
      return 0;
    }
  }
  int awal = 3;
  for (int i = 1; i <= 3; i++) {
    if (dig[i] > 0) {
      awal = i;
      break;
    }
  }
  if ((3 - awal + 1) == n || n == 1) {
    for (int i = 1; i <= 3; i++) {
      cout << max(dig[i], 0);
    }
    cout << '\n';
    return 0;
  }
  cout << -1 << '\n';
  return 0;
}
