#include <bits/stdc++.h>
using namespace std;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 100000000;
const long long LINF = 1000000000000000000;
const long long MOD = 998244353;
const double EPS = 1e-6;
using pii = std::pair<int, int>;
using ll = long long;
using pLL = std::pair<ll, ll>;
constexpr auto PI = 3.14159265358979323846264338327950L;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<pii> a(M);
  for (int i = 0; i < M; ++i) {
    cin >> a[i].first >> a[i].second;
  }
  std::sort(a.begin(), a.end());
  if (a[0].first == 1 && a[0].second == 0 && N != 1) {
    cout << -1 << endl;
    return 0;
  }
  int ans = 0;
  set<int> x, y, z;
  for (int i = 0; i < M; ++i) {
    if (a[i].first == 1) {
      x.insert(a[i].second);
    } else if (a[i].first == 2) {
      y.insert(a[i].second);
    } else if (a[i].first == 3) {
      z.insert(a[i].second);
    }
  }
  if (x.size() <= 1 && y.size() <= 1 && z.size() <= 1) {
    if (x.size() != 0 && y.size() != 0 && z.size() != 0) {
      cout << *x.begin() << *y.begin() << *z.begin() << endl;
    } else if (x.size() == 0 && y.size() != 0 && z.size() != 0) {
      cout << *y.begin() << *z.begin() << endl;
    } else if (x.size() != 0 && y.size() == 0 && z.size() != 0) {
      cout << *x.begin() << 0 << *z.begin() << endl;
    } else if (x.size() != 0 && y.size() != 0 && z.size() == 0) {
      cout << *x.begin() << *y.begin() << 0 << endl;
    } else if (x.size() == 0 && y.size() == 0 && z.size() != 0) {
      cout << *z.begin() << endl;
    } else if (x.size() == 0 && y.size() != 0 && z.size() == 0) {
      cout << *y.begin() << 0 << endl;
    } else if (x.size() != 0 && y.size() == 0 && z.size() == 0) {
      cout << *x.begin() << 0 << 0 << endl;
    } else {
      cout << -1 << endl;
    }
  } else {
    cout << -1 << endl;
  }
  return 0;
}
