#include <bits/stdc++.h>
using namespace std;
int main() {
  long long i, j, k, a = 0, s = 0, b, n, m;
  cin >> n >> m;
  long long xx[n];
  vector<pair<long long, long long> > v;
  for (i = 1; i <= n; i++) xx[i] = -1;
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    v.push_back(make_pair(a, b));
  }
  sort(v.begin(), v.end());
  long long f = 0;
  vector<long long> v1, v2, v3;
  for (i = 0; i < m; i++) {
    if (v[i].first == 1) {
      v1.push_back(v[i].second);
    }
    if (v[i].first == 2) {
      v2.push_back(v[i].second);
    }
    if (v[i].first == 3) {
      v3.push_back(v[i].second);
    }
  }
  for (i = 0; i < v1.size(); i++) {
    if (v1[0] != v1[i]) f = 1;
  }
  for (i = 0; i < v2.size(); i++) {
    if (v2[0] != v2[i]) f = 1;
  }
  for (i = 0; i < v3.size(); i++) {
    if (v3[0] != v3[i]) f = 1;
  }
  if (f == 1) {
    cout << -1;
    return 0;
  }
  long long ss[3];
  if (v1.size() != 0)
    ss[0] = v1[0];
  else
    ss[0] = 0;
  if (v2.size() != 0)
    ss[1] = v2[0];
  else
    ss[1] = 0;
  if (v3.size() != 0)
    ss[2] = v3[0];
  else
    ss[2] = 0;
  if (n == 1 && ss[0] == 0 && ss[1] == 0 && ss[2] == 0) {
    cout << 0;
    return 0;
  }
  if (n == 3 && v1.size() > 0 && ss[0] != 0)
    cout << ss[0] << ss[1] << ss[2];
  else if (n == 3 && (ss[0] == 0 || v1.size() == 0))
    cout << -1;
  if (n == 2 && (v1.size() == 0) && v2.size() != 0 && v2[0] != 0)
    cout << ss[1] << ss[2];
  else if (n == 2 && (v1.size() > 0 || v2.size() == 0 || v2[0] == 0))
    cout << -1;
  if (n == 1 && v1.size() == 0 && v2.size() == 0)
    cout << ss[2];
  else if (n == 1 && (v1.size() > 0 || v2.size() > 0))
    cout << -1;
}
