#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(10);
  int N, M;
  cin >> N >> M;
  map<int, int> num;
  for (long long i = 0; i < (long long)(M); i++) {
    int s, c;
    cin >> s >> c;
    if (num.count(s)) {
      if (num.at(s) != c) {
        cout << -1 << endl;
        return 0;
      }
    }
    num.emplace(s, c);
  }
  if (num.size() == 0) {
    cout << -1 << endl;
    return 0;
  }
  if (N > 1 && num.count(1) == 1 && num.at(1) == 0) {
    cout << -1 << endl;
    return 0;
  }
  if (N == 1 && ((num.count(2)) != 0 || (num.count(3) != 0))) {
    cout << -1 << endl;
    return 0;
  }
  if (N == 2 && (num.count(3) != 0)) {
    cout << -1 << endl;
    return 0;
  }
  if (num.count(1)) cout << num.at(1);
  if (num.count(2) == 1) {
    cout << num.at(2);
  } else if (N >= 2) {
    cout << 0;
  }
  if (num.count(3) == 1) {
    cout << num.at(3);
  } else if (N >= 3) {
    cout << 0;
  }
  return 0;
}
