#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
template <class T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
struct aaa {
  aaa() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} aaa;
bool IsPrime(int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false;
  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      return false;
    }
  }
  return true;
}
int n, k, w, h;
string s;
void Main() {
  int n, m;
  cin >> n >> m;
  if (m == 0) {
    cout << -1;
    return;
  }
  int one = 0, ten = 0, hand = 0;
  bool f1 = 1, f2 = 1, f3 = 1;
  vector<pair<int, int>> v(m);
  for (int i = int(0); i < int(m); ++i) cin >> v[i].first >> v[i].second;
  sort((v).begin(), (v).end());
  reverse((v).begin(), (v).end());
  if (v[0].first != n) {
    cout << -1;
    return;
  }
  for (int i = int(0); i < int(m); ++i) {
    if (v[i].first == 1) {
      if (f1) {
        one = v[i].second;
        f1 = !f1;
      } else {
        if (one == v[i].second) continue;
        cout << -1;
        return;
      }
    } else if (v[i].first == 2) {
      if (f2) {
        ten = v[i].second;
        f2 = !f2;
      } else {
        if (ten == v[i].second) continue;
        cout << -1;
        return;
      }
    } else if (v[i].first == 3) {
      if (f3) {
        hand = v[i].second;
        f3 = !f3;
      } else {
        if (hand == v[i].second) continue;
        cout << -1;
        return;
      }
    }
  }
  cout << one * 100 + ten * 10 + hand;
}
signed main() {
  Main();
  return 0;
}
