#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int keta(int x) {
  if (x == 0) return 0;
  return 1 + keta(x / 10);
}
int func(int i, int x) {
  if (i == 0) return x % 10;
  return func(i - 1, x / 10);
}
void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> s(m), c(m);
  for (int i = 0; i < (m); ++i) cin >> s[i] >> c[i];
  for (int i = 1; i < 1000; i++) {
    if (keta(i) != n) continue;
    bool b = true;
    for (int j = 0; j < (m); ++j) {
      if (func(n - s[j], i) != c[j]) b = false;
    }
    if (b) {
      cout << i << endl;
      return;
    }
  }
  cout << -1 << endl;
}
int main() {
  solve();
  return 0;
}
