#include <bits/stdc++.h>
using namespace std;
template <typename T>
std::string NumberToString(T Number) {
  std::ostringstream ss;
  ss << Number;
  return ss.str();
}
template <typename T>
T StringToNumber(const string &Text) {
  istringstream ss(Text);
  T result;
  return ss >> result ? result : 0;
}
string StrCpy(string str, long long int start, long long int end) {
  string temp = "";
  for (long long int i = start; i <= end; i++) {
    temp += str[i];
  }
  return temp;
}
int solve() {
  int arr[4];
  arr[0] = 10;
  arr[1] = 10;
  arr[2] = 10;
  int n, m;
  int s, c;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> s >> c;
    if (arr[s - 1] > c) arr[s - 1] = c;
  }
  if (arr[0] == 10 || arr[0] == 0)
    cout << "-1" << endl;
  else {
    if (arr[1] == 10)
      arr[1] = 0;
    else if (arr[2] == 10)
      arr[2] = 0;
    cout << arr[0] << arr[1] << arr[2] << endl;
  }
  return 0;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  for (auto blockTime = make_pair(chrono::high_resolution_clock::now(), true);
       blockTime.second;
       fprintf(stderr, "%s: %.5lf s\n", "programm time",
               chrono::duration_cast<chrono::milliseconds>(
                   chrono::high_resolution_clock::now() - blockTime.first)
                       .count() /
                   1000.0),
            fflush(stderr), blockTime.second = false) {
    solve();
  }
  return 0;
}
