#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<std::pair<int, int> > p(m);
  map<int, int> mp;
  for (int i = 1; i <= 3; i++) {
    mp[i] = 0;
  }
  for (int i = 0; i < m; i++) {
    vector<int> v(2);
    for (int j = 0; j < 2; j++) {
      cin >> v[j];
    }
    std::pair<int, int> p1(v[0], v[1]);
    p[i] = p1;
  }
  string ans = "";
  for (int i = 0; i < p.size(); i++) {
    if (p[i].first == 1 && p[i].second == 0 && n != 1) {
      ans = "-1";
      break;
    }
    if (mp[p[i].first] == 0 || mp[p[i].first] == p[i].second) {
      mp[p[i].first] = p[i].second;
    } else {
      ans = "-1";
      break;
    }
  }
  if (ans == "-1")
    cout << ans << endl;
  else {
    for (int i = 1; i <= 3; i++) {
      ans += to_string(mp[i]);
    }
    if (stoi(ans) < 0 || ans[0] == '0')
      cout << "-1" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
