#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int N, M;
  cin >> N >> M;
  vector<int> s(M), c(M);
  vector<pair<int, int>> p(M);
  for (int i = 0; i < M; i++) {
    cin >> s[i] >> c[i];
    p[i] = make_pair(s[i], c[i]);
  }
  sort(p.begin(), p.end());
  if (N == 1) {
    for (int i = 0; i < M; i++) {
      if (p[i].first != 1) {
        cout << -1 << endl;
        return 0;
      }
    }
    for (int i = 0; i < M - 1; i++) {
      if (p[i].first == p[i + 1].first && p[i].second != p[i + 1].second) {
        cout << -1 << endl;
        return 0;
      }
    }
    cout << p[0].first << endl;
  } else if (N == 2) {
    for (int i = 0; i < M; i++) {
      if (p[i].first == 1 && p[i].second == 0) {
        cout << -1 << endl;
      }
    }
    for (int i = 0; i < M - 1; i++) {
      if (p[i].first == p[i + 1].first && p[i].second != p[i + 1].second) {
        cout << -1 << endl;
        return 0;
      }
    }
    for (int i = 0; i < M; i++) {
      if (p[i].first == 1) {
        cout << p[i].second;
        break;
      }
    }
    bool flag = false;
    for (int i = 0; i < M; i++) {
      if (p[i].first == 2) {
        cout << p[i].second;
        flag = true;
        break;
      }
    }
    if (flag == false) cout << 0;
  } else if (N == 3) {
    for (int i = 0; i < M; i++) {
      if (p[i].first == 1 && p[i].second == 0) {
        cout << -1 << endl;
        return 0;
      }
    }
    for (int i = 0; i < M - 1; i++) {
      if (p[i].first == p[i + 1].first && p[i].second != p[i + 1].second) {
        cout << -1 << endl;
        return 0;
      }
    }
    for (int i = 0; i < M; i++) {
      if (p[i].first == 1) {
        cout << p[i].second;
        break;
      }
    }
    bool flag = false;
    for (int i = 0; i < M; i++) {
      if (p[i].first == 2) {
        cout << p[i].second;
        flag = true;
        break;
      }
    }
    if (flag == false) cout << 0;
    flag = false;
    for (int i = 0; i < M; i++) {
      if (p[i].first == 3) {
        cout << p[i].second;
        flag = true;
        break;
      }
    }
    if (flag == false) cout << 0;
  }
}
