#include <bits/stdc++.h>
using namespace std;
struct Fast {
  Fast() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} fast;
long long mod = 1000000007;
long long modpow(long long m, long long n) {
  if (n == 0) return 1;
  if (n % 2 == 0) {
    long long t = modpow(m, n / 2);
    return t / 2;
  } else {
    return modpow(m, n - 1) * m % mod;
  }
}
int main() {
  long long n, m, a, b, d1 = 0, d2 = 0, d3 = 0, ans = 0, sum = 0;
  cin >> n >> m;
  vector<int> as(m);
  vector<int> bs(m);
  map<int, int> cnt;
  for (int i = 0; i < m; i++) {
    cin >> as[i];
    cin >> bs[i];
  }
  for (int i = 0; i < m; i++) {
    cnt[as[i]]++;
  }
  for (int i = 0; i < m; i++) {
    if (cnt[as[i]] > 1) {
      a = as[i];
      b = bs[i];
      for (int j = 0; j < m; j++) {
        if (as[j] == a) {
          if (bs[j] != b) {
            cout << "-1" << endl;
            return 0;
          }
        }
      }
    }
  }
  for (int i = 0; i < m; i++) {
    if (as[i] == 1) {
      d1 = bs[i];
    }
    if (as[i] == 2) {
      d2 = bs[i];
    }
    if (as[i] == 3) {
      d3 = bs[i];
    }
  }
  if (d1 == 0) {
    if (n == 3) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << d1 * 100 + d2 * 10 + d3 << endl;
}
