#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  int S[5], C[5];
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> S[i] >> C[i];
  }
  int Number[3] = {0};
  int Answer = -1;
  for (int i = 0; i < 1000; i++) {
    switch (N) {
      case 1:
        if (i >= 10) {
          break;
        }
      case 2:
        if (i < 10)
          continue;
        else if (i >= 100) {
          break;
        }
      case 3:
        if (i < 100) continue;
    }
    Number[2] = i % 10;
    Number[1] = i / 10 % 10;
    Number[0] = i / 100;
    for (int j = 0; j < M; j++) {
      if (C[j] != Number[S[j] - 1]) break;
      if (j == M - 1) {
        Answer = i;
        goto loopend;
      }
    }
  }
loopend:
  cout << Answer << endl;
  ;
}
