#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int main(int argc, char const* argv[]) {
  int n, m;
  cin >> n >> m;
  vector<bool> f(3, false);
  vector<int> tmp(3, 0);
  for (int i = 0; i < (m); i++) {
    int s, c;
    cin >> s >> c;
    if (s == 1 && c == 0) {
      cout << "-1" << endl;
      return 0;
    }
    s--;
    if (f[s] && c != tmp[s]) {
      cout << "-1" << endl;
      return 0;
    } else {
      f[s] = true;
      tmp[s] = c;
    }
  }
  if (n == 3 && !f[0]) {
    cout << "-1" << endl;
    return 0;
  }
  if (n == 2 && !f[1]) {
    cout << "-1" << endl;
    return 0;
  }
  if (n == 1 && !f[2]) {
    cout << "-1" << endl;
    return 0;
  }
  int ans = 0;
  int rec = 100 * tmp[0] + 10 * tmp[1] + tmp[2];
  for (int i = 0; i < (1000); i++) {
    if (i >= rec) {
      ans = i;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
