#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e9 + 7;
const int mod = 1e9 + 7;
int s[5], c[5], a[3];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> s[i] >> c[i];
    if (a[s[i] - 1] == 0)
      a[s[i] - 1] = c[i];
    else {
      a[s[i] - 1] = min(c[i], a[s[i] - 1]);
    }
  }
  if (a[0] == 0) {
    cout << -1;
  } else {
    for (int i = 0; i < 3; i++) cout << a[i];
  }
}
