#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
const double EPS = 1e-6;
vector<int> v1, v2, v3;
int a[4];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    if (x == 1)
      v1.push_back(y);
    else if (x == 2)
      v2.push_back(y);
    else if (x == 3)
      v3.push_back(y);
  }
  if (v1.size() != 0) sort(v1.begin(), v1.end());
  if (v2.size() != 0) sort(v2.begin(), v2.end());
  if (v3.size() != 0) sort(v3.begin(), v3.end());
  if (v1.size() != 0) a[1] = v1[0];
  if (v2.size() != 0) a[2] = v2[0];
  if (v3.size() != 0) a[3] = v3[0];
  if (n == 1)
    cout << a[0] << endl;
  else {
    int flag = 0;
    for (int i = 1; i <= n; i++) {
      if (a[i] != 0) {
        flag = 1;
      } else {
        if (!flag) {
          cout << -1 << endl;
          return 0;
        }
      }
    }
    cout << a[1] << a[2] << a[3] << endl;
  }
  return 0;
}
