#include <bits/stdc++.h>
using namespace std;
set<int> s;
int A[5][5], B[5][5];
vector<int> a[5];
int main() {
  bool p = 0;
  int n, m;
  cin >> n >> m;
  int c, s;
  for (int i = 1; i <= m; i++) {
    cin >> c >> s;
    if (c == 1 && s) p = 1;
    a[c].push_back(s);
  }
  for (int i = 1; i <= 3; i++) sort(a[i].begin(), a[i].end());
  if (!p)
    cout << -1 << endl;
  else {
    for (auto i : a[1]) {
      if (i != 0) cout << i;
      break;
    };
    if (!a[2].size())
      cout << 0;
    else
      cout << a[2][0];
    if (!a[3].size())
      cout << 0 << endl;
    else
      cout << a[3][0];
  }
  return 0;
}
