#include <bits/stdc++.h>
using namespace std;
vector<int> d(3);
void digit(int x) {
  int i = 0;
  while (x) {
    d[i] = x % 10;
    x /= 10;
    i++;
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> s(m), c(m);
  for (int i = 0; i < (int)(m); i++) cin >> s[i] >> c[i];
  for (int x = 1; x < 1000; x++) {
    bool ex = false;
    digit(x);
    for (int i = 0; i < (int)(m); i++) {
      for (int j = 0; j < (int)(n); j++) {
        ex = false;
        if (s[i] == d[j]) ex = true;
      }
    }
    if (ex) {
      cout << x << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}
