#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
signed main() {
  vector<long long> v(3, INF);
  long long n, m;
  cin >> n >> m;
  for (long long i = 0; i < m; i++) {
    long long s, c;
    cin >> s >> c;
    s--;
    if (v[s] != INF && v[s] != c) {
      cout << -1 << endl;
      return 0;
    }
    v[s] = min(v[s], c);
  }
  long long ans = INF;
  for (long long i = 999; i >= 0; i--) {
    long long res = 0;
    string t = to_string(i);
    if (v[0] == INF || v[0] == t[0] - '0') res++;
    if (v[1] == INF || v[1] == t[1] - '0') res++;
    if (v[2] == INF || v[2] == t[2] - '0') res++;
    if (res == 3) ans = min(ans, i);
  }
  if (ans == 0 && (v[0] == 0 || v[1] == 0)) ans = -1;
  if (v[0] == 0) ans = -1;
  cout << ans << endl;
  return 0;
}
