#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vll = vector<ll>;
using P = pair<int, int>;
const int INF = 1e9;
const ll INFLL = 1e18;
const int MOD = 1e9 + 7;
const int NIL = -1;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
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;
  sort(begin(p), end(p), [](P &a, P &b) { return a.first < b.first; });
  vi d(3, INF);
  for (int i = 0; i < (m); ++i) {
    if (p[i].first == 1) chmin(d[0], p[i].second);
    if (p[i].first == 2) chmin(d[1], p[i].second);
    if (p[i].first == 3) chmin(d[2], p[i].second);
  }
  for (int i = 0; i < (3); ++i) {
    if ((d[0] == INF || d[0] == 0) && n != 1) {
      cout << NIL << endl;
      break;
    } else if (d[i] != INF)
      cout << d[i];
    else if (d[i] == INF)
      cout << 0;
  }
  return 0;
}
