#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
unsigned long long gcd(unsigned long long a, unsigned long long b) {
  return b ? gcd(b, a % b) : a;
}
unsigned long long lcm(unsigned long long a, unsigned long long b) {
  return a / gcd(a, b) * b;
}
void func(long long N, long long M, std::vector<long long> s,
          std::vector<long long> c) {
  ll d = 0;
  ll e = 0;
  ll f = 0;
  for (int i = 0; i < M; i++) {
    if (s[i] == 1 && d == 0) {
      d = c[i];
    } else if (s[i] == 1) {
      d = std::min(d, c[i]);
    }
    if (s[i] == 2) {
      if (e == 0) {
        e = c[i];
      } else {
        e = min(e, c[i]);
      }
    }
    if (s[i] == 3) {
      if (f == 0) {
        f = c[i];
      } else {
        f = min(f, c[i]);
      }
    }
  }
  if (N == 1) {
    cout << d << endl;
    return;
  } else if (N == 2 && d > 0) {
    cout << d << e << endl;
    return;
  } else if (N == 3 && d > 0) {
    cout << d << e << f << endl;
    return;
  }
  cout << -1 << endl;
}
int main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  std::vector<long long> s(M);
  std::vector<long long> c(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &s[i]);
    scanf("%lld", &c[i]);
  }
  func(N, M, std::move(s), std::move(c));
  return 0;
}
