#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  int x0 = 0;
  int x1 = 0;
  int x2 = 0;
  bool br = false;
  string ans = "100";
  char s, c;
  for (int i = 0; i < N; i++) {
    cin >> s >> c;
    if (s == '1') {
      if (x0 == 0 || ans[0] == c) {
        ans[0] = c;
        x0++;
      } else {
        br = true;
        break;
      }
    }
    if (s == '2') {
      if (x1 == 0 || ans[1] == c) {
        ans[1] = c;
        x1++;
      } else {
        br = true;
        break;
      }
    }
    if (s == '3') {
      if (x2 == 0 || ans[2] == c) {
        ans[2] = c;
        x2++;
      } else {
        br = true;
        break;
      }
    }
  }
  if (br)
    cout << -1 << endl;
  else if (N == 3 && ans[0] == '0')
    cout << -1 << endl;
  else if (N == 2 && ans[1] == '0')
    cout << -1 << endl;
  else
    cout << stoi(ans) << endl;
}
