#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int n, m;
  cin >> n >> m;
  vector<P> p(m);
  for (int i = 0; i < (m); i++) {
    cin >> p[i].first >> p[i].second;
  }
  for (int i = 0; i < (1000); i++) {
    int nx = i / 10;
    vector<int> digit(1, i % 10);
    while (nx) {
      digit.push_back(nx % 10);
      nx /= 10;
    }
    if (digit.size() != n) continue;
    bool flag = true;
    reverse(digit.begin(), digit.end());
    for (int i = 0; i < (m); i++) {
      if (digit[p[i].first - 1] != p[i].second) flag = false;
      if (flag) {
        cout << i << endl;
        return 0;
      }
    }
  }
  cout << -1 << endl;
  return 0;
}
