#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> s(m);
  vector<int> c(m);
  for (int(i) = 0; (i) < (m); (i)++) cin >> s[i] >> c[i];
  vector<int> x(n);
  int min = 10, min2 = 10, min3 = 10;
  int cnt = 0;
  for (int(i) = 0; (i) < (m); (i)++) {
    if (n == s[i]) {
      cnt++;
    }
  }
  if (cnt == 0) {
    cout << -1 << endl;
    return 0;
  }
  for (int(i) = 0; (i) < (m); (i)++) {
    if (s[i] == 1) {
      if (min > c[i]) {
        min = c[i];
        x.insert(x.begin(), c[i]);
      }
    } else if (s[i] == 2) {
      if (min2 > c[i]) {
        min2 = c[i];
        x.insert(x.begin() + 1, c[i]);
      }
    } else if (s[i] == 3) {
      if (min3 > c[i]) {
        min3 = c[i];
        x.insert(x.begin() + 2, c[i]);
      }
    }
  }
  if (x[0] != 0) {
    cout << x[0];
  }
  if (x[0] != 0 && x[1] == 0) {
    cout << x[1];
  }
  cout << x[2] << endl;
  return 0;
}
