#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> vp(4);
  for (int i = 0; i < (m); i++) {
    int s, c;
    cin >> s >> c;
    if (vp[s].first == c && vp[s].second == 1) continue;
    vp[s].first = c;
    vp[s].second = 1;
  }
  bool flg = true;
  for (auto x : vp)
    if (x.second >= 2) flg = false;
  if (!flg || vp[1].first == 0)
    cout << -1 << endl;
  else
    for (int i = 1; i < 4; i++) cout << vp[i].first;
  cout << endl;
}
