#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int main(int argc, char const* argv[]) {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > mp(m);
  for (int i = 0; i < (m); i++) {
    cin >> mp[i].first >> mp[i].second;
  }
  sort((mp).begin(), (mp).end());
  int tmp = 0, p1 = 10, p2 = 10, p3 = 10;
  for (int i = 0; i < (m - 1); i++) {
    if (mp[i].first == mp[i + 1].first && mp[i].second != mp[i + 1].second) {
      cout << "-1" << endl;
      return 0;
    }
  }
  for (int i = 0; i < (m); i++) {
    if (mp[i].first == 1) chmin(p1, mp[i].second);
    if (mp[i].first == 2) chmin(p2, mp[i].second);
    if (mp[i].first == 3) chmin(p3, mp[i].second);
    if (mp[i].first == 1 && mp[i].second == 0) {
      cout << "-1" << endl;
      return 0;
    }
  }
  if (p1 == 10) p1 = 0;
  if (p2 == 10) p2 = 0;
  if (p2 == 10) p3 = 0;
  tmp += 100 * p1 + 10 * p2 + p3;
  int ans = -1;
  for (int i = 0; i < (1000); i++) {
    if (i >= tmp) {
      ans = i;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
