#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;
using ii = pair<int, int>;
using ll = long long;
using vi = vector<int>;
using vd = vector<double>;
using vb = vector<bool>;
using vl = vector<ll>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvb = vector<vector<bool>>;
using vii = vector<pair<int, int>>;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return (a * b) / GCD(a, b); }
int round_int(int a, int b) { return (a + (b - 1)) / b; }
vector<pair<ll, int>> factorize(ll n) {
  vector<pair<ll, int>> res;
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i) continue;
    res.emplace_back(i, 0);
    while (n % i == 0) {
      n /= i;
      res.back().second++;
    }
  }
  if (n != 1) res.emplace_back(n, 1);
  return res;
}
constexpr array<int, 9> dx = {0, 1, 0, -1, -1, 1, 1, -1, 0};
constexpr array<int, 9> dy = {1, 0, -1, 0, 1, 1, -1, -1, 0};
constexpr auto MAX = 100010;
vi f(int v) {
  vi res(3, -1);
  int i = 0;
  while (v) {
    res[i] = v % 10;
    v /= 10;
    i++;
  }
  return res;
}
int main() {
  int n, m;
  cin >> n >> m;
  vi s(m), c(m);
  for (int i = 0; i < (m); i++) {
    cin >> s[i] >> c[i];
    s[i] = n - s[i];
  }
  for (int i = 0; i < (1000); i++) {
    vi cut = f(i);
    bool flg = true;
    for (int j = 0; j < (m); j++) {
      if (cut[s[j]] != -1 && cut[s[j]] == c[j]) continue;
      flg = false;
    }
    if (flg) {
      cout << i << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}
