#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, m;
  cin >> n >> m;

  //ベクトル配列の入力
  vector<int> s(m);
  vector<string> c(m);
  for (int i = 0; i < n; i++) cin >> s[i] >> c[i];

  int ans = -1;
  bool exist = true;
  if (n==1) {
    for (int num = 0; num <= 9; num++) {
      string str = to_string(num);
      for (int i = 0; i < m; i++) {
        if (exist==true && string[s[j]-1] == c[i]) exist = true;
        else {
          exist = false;
          break;
        }
      }
      if (exist) {
        ans = num;
        break;
      }
    }
  } else if (n==2) {
    for (int num = 10; num <= 99; num++) {
      string str = to_string(num);
      for (int i = 0; i < m; i++) {
        if (exist==true && string[s[j]-1] == c[i]) exist = true;
        else {
          exist = false;
          break;
        }
      }
      if (exist) {
        ans = num;
        break;
      }
    }
  } else {
    for (int num = 100; num <= 999; num++) {
      string str = to_string(num);
      for (int i = 0; i < m; i++) {
        if (exist==true && string[s[j]-1] == c[i]) exist = true;
        else {
          exist = false;
          break;
        }
      }
      if (exist) {
        ans = num;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
