#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1001001001;
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll isprime(ll n) {
  bool prime = true;
  for (int i = 2; i < n; i++) {
    if (n % i == 0) {
      prime = false;
      break;
    }
  }
  if (prime) {
    cout << n << " is prime" << endl;
  } else {
    cout << n << " is not prime" << endl;
  }
}
ll factorial(ll n) {
  ll a = 1;
  ll b = n;
  for (int i = 0; i < n; i++) {
    a *= b;
    b--;
  }
  return a;
}
ll comb(ll a, ll b) {
  ll c;
  c = factorial(a) / (factorial(a - b) * factorial(b));
  return c;
}
int main() {
  cout << fixed << setprecision(10);
  int N, M;
  cin >> N >> M;
  vector<int> vecs(M);
  vector<int> vecc(M);
  vector<vector<int>> vecsc(M, vector<int>(2));
  bool exist = true;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < 2; j++) {
      cin >> vecsc[i][j];
    }
  }
  sort(vecsc.begin(), vecsc.end());
  for (int i = 0; i < M - 1; i++) {
    if (vecsc[i][0] == vecsc[i + 1][0]) {
      if (vecsc[i][1] != vecsc[i + 1][1]) {
        exist = false;
        break;
      }
    }
  }
  if ((N == 3 || N == 2) && vecsc[0][0] == 1 & vecsc[0][1] == 0) {
    exist = false;
  }
  int x, y, z;
  y = 0;
  z = 0;
  for (int i = 0; i < M; i++) {
    if (vecsc[i][0] == 1) {
      x = vecsc[i][1];
    }
    if (vecsc[i][0] == 2) {
      y = vecsc[i][1];
    }
    if (vecsc[i][0] == 3) {
      z = vecsc[i][1];
    }
  }
  if (exist) {
    cout << x << y << z << endl;
  } else {
    cout << "-1" << endl;
  }
}
