#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int ex(int x, int n) {
  int y;
  for (ll i = 0; i < 4 - n; i++) {
    y = x % 10;
    x /= 10;
  }
  return y;
}
int main() {
  int n, m, ans;
  cin >> n >> m;
  bool x = false;
  vector<int> s(n), c(n);
  for (ll i = 0; i < n; i++) cin >> s[i] >> c[i];
  if (n == 1) {
    for (int i = 0; i < 10; i++) {
      if (ex(i, s[0]) == c[0]) {
        ans = i;
        x = true;
        break;
      }
    }
  }
  if (n == 2) {
    for (int i = 10; i < 100; i++) {
      if (ex(i, s[0]) == c[0] && ex(i, s[1]) == c[1]) {
        ans = i;
        x = true;
        break;
      }
    }
  }
  if (n == 3) {
    for (int i = 100; i < 1000; i++) {
      if (ex(i, s[0]) == c[0] && ex(i, s[1]) == c[1] && ex(i, s[2]) == c[2]) {
        ans = i;
        x = true;
        break;
      }
    }
  }
  if (x)
    cout << ans << endl;
  else
    cout << -1 << endl;
  return 0;
}
