#include <bits/stdc++.h>
using namespace std;
int main() {
  long long i, j, k, a = 0, s = 0, b, n, m;
  cin >> n >> m;
  long long xx[n];
  vector<pair<long long, long long> > v;
  for (i = 1; i <= n; i++) xx[i] = -1;
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    v.push_back(make_pair(a, b));
  }
  sort(v.begin(), v.end());
  long long f = 0;
  vector<long long> v1, v2, v3;
  for (i = 0; i < m; i++) {
    if (v[i].first == 1) {
      v1.push_back(v[i].second);
    }
    if (v[i].first == 2) {
      v2.push_back(v[i].second);
    }
    if (v[i].first == 3) {
      v3.push_back(v[i].second);
    }
  }
  for (i = 0; i < v1.size(); i++) {
    if (v1[0] != v1[i]) f = 1;
  }
  for (i = 0; i < v2.size(); i++) {
    if (v2[0] != v2[i]) f = 1;
  }
  for (i = 0; i < v3.size(); i++) {
    if (v3[0] != v3[i]) f = 1;
  }
  if (f == 1) {
    cout << -1;
    return 0;
  }
  long long u = 0, r = 0, e = 0;
  if (v1.size() != 0) u = 1;
  if (v2.size() != 0) r = 1;
  if (v3.size() != 0) e = 1;
  if (n != u + r + e)
    cout << -1;
  else {
    if (v1.size() != 0)
      cout << v1[0];
    else
      cout << 0;
    if (v2.size() != 0)
      cout << v2[0];
    else
      cout << 0;
    if (v3.size() != 0)
      cout << v3[0];
    else
      cout << 0;
  }
}
