#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int x, y;
  int arr[3];
  for (int i = 1; i <= 3; i++) {
    arr[i] = 0;
  }
  map<int, int> mp;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    mp.insert(make_pair(x, y));
  }
  int c = 0;
  for (auto it = mp.begin(); it != mp.end(); it++) {
    arr[it->first] = it->second;
    c++;
  }
  if (c > 1) {
    for (int i = 1; i <= 3; i++) {
      cout << arr[i];
    }
  } else if (c == 1) {
    cout << "-1" << endl;
  }
}
