#include <bits/stdc++.h>
using namespace std;
int main() {
  int Flg = 0;
  int N, M;
  cin >> N >> M;
  int range = pow(10, N);
  int s[M];
  int c[M];
  for (int i = 0; i < M; i++) {
    cin >> s[i] >> c[i];
  }
  for (int i = 0; i < range; i++) {
    int x = i;
    int num[3] = {0, 0, 0};
    int count = 0;
    while (true) {
      int r = x % 10;
      int q = x / 10;
      num[count] = r;
      x = q;
      if (q == 0) break;
      ++count;
    }
    int a = num[0];
    num[0] = num[2];
    num[2] = a;
    for (int i = 0; i < M; i++) {
      if (num[s[i] - 1] == c[i])
        Flg = 1;
      else {
        Flg = 2;
        break;
      }
    }
    if (Flg == 1) {
      cout << i;
      break;
    }
  }
  if (Flg == 2) {
    cout << -1;
  }
  return 0;
}
