#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll factorial(int i) {
  if (i == 0) return 1;
  return (factorial(i - 1)) * i;
}
int d(ll n) {
  int count = 0;
  while (n != 0) {
    n /= 10;
    count++;
  }
  return count;
}
int main() {
  int n, m;
  cin >> n >> m;
  map<int, int> ma;
  for (int i = 0; i < (int)(m); i++) {
    int a, b;
    cin >> a >> b;
    a = n - a;
    if (ma.count(a) && ma[a] != b) {
      cout << -1 << endl;
      return 0;
    }
    ma[a] = b;
  }
  for (int i = ma[2] * 100 + ma[1] * 10 + ma[0] * 1; i < 1000; i++) {
    if (d(i) == n && i / 100 == ma[2] && i / 10 % 10 == ma[1] &&
        i % 10 == ma[0]) {
      cout << i << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
