#include <bits/stdc++.h>
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;
using ll = long long;
using vi = std::vector<int>;
using vc = std::vector<char>;
using vll = std::vector<long long>;
using vs = std::vector<string>;
using Mi = map<int, int>;
using Mll = map<ll, ll>;
using UMi = unordered_map<int, int>;
using UMll = unordered_map<ll, ll>;
using Pi = pair<int, int>;
using Pll = pair<ll, ll>;
using vPi = vector<Pi>;
using vPll = vector<Pll>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<ll>>;
using vvc = vector<vector<char>>;
using vvs = vector<vector<string>>;
using pqgi = priority_queue<int, vector<int>, greater<int>>;
using pqsi = priority_queue<int, vector<int>, less<int>>;
using pqgll = priority_queue<int, vector<int>, greater<int>>;
using pssll = priority_queue<int, vector<int>, less<int>>;
template <class T>
using vec = vector<T>;
long long N, M;
signed main() {
  cin >> N;
  cin >> M;
  if (N == 1) {
    vector<long long> numbers;
    for (long long i = 0; i < M; i++) {
      long long s, c;
      cin >> s >> c;
      numbers.emplace_back(c);
    }
    sort((numbers).begin(), (numbers).end());
    if (M == 0) {
      cout << 0 << "\n";
      return 0;
    }
    if (numbers[0] != numbers[numbers.size() - 1]) {
      cout << -1 << "\n";
      return 0;
    } else {
      cout << numbers[0] << "\n";
    }
  } else if (N == 2) {
    vector<vector<long long>> numbers;
    numbers.resize(2);
    for (long long i = 0; i < M; i++) {
      long long s, c;
      cin >> s >> c;
      s--;
      numbers[s].emplace_back(c);
    }
    for (long long i = 0; i < 2; i++) {
      sort((numbers[i]).begin(), (numbers[i]).end());
    }
    long long min_result[2] = {0, 0};
    if (M == 0) {
      cout << 10 << "\n";
      return 0;
    }
    for (long long i = 0; i < 2; i++) {
      if (numbers[i].size() == 0) {
        continue;
      }
      if (numbers[i][0] != numbers[i][numbers[i].size() - 1]) {
        cout << -1 << "\n";
        return 0;
      }
      if ((i == 0) && (numbers[i][0] == 0)) {
        cout << -1 << "\n";
        return 0;
      }
      min_result[i] = numbers[i][0];
    }
    cout << min_result[0] * 10 + min_result[1] << "\n";
  } else {
    vector<vector<long long>> numbers;
    numbers.resize(3);
    for (long long i = 0; i < M; i++) {
      long long s, c;
      cin >> s >> c;
      s--;
      numbers[s].emplace_back(c);
    }
    for (long long i = 0; i < 3; i++) {
      sort((numbers[i]).begin(), (numbers[i]).end());
    }
    long long min_result[3] = {0, 0, 0};
    if (M == 0) {
      cout << 100 << "\n";
      return 0;
    }
    for (long long i = 0; i < 3; i++) {
      if (numbers[i].size() == 0) {
        continue;
      }
      if (numbers[i][0] != numbers[i][numbers[i].size() - 1]) {
        cout << -1 << "\n";
        return 0;
      }
      if ((i == 0) && (numbers[i][0] == 0)) {
        cout << -1 << "\n";
        return 0;
      }
      min_result[i] = numbers[i][0];
    }
    cout << min_result[0] * 100 + min_result[1] * 10 + min_result[2] << "\n";
  }
  return 0;
}
