#include <bits/stdc++.h>
using namespace std;
using LL = long long;
template <class T>
void sort(vector<T> &v) {
  sort(v.begin(), v.end());
}
template <class T>
void rsort(vector<T> &v) {
  sort(v.rbegin(), v.rend());
}
int main() {
  int n;
  cin >> n;
  int m;
  cin >> m;
  int s[m], c[m];
  for (int i = 0; i < m; i++) cin >> s[i] >> c[i];
  set<int> one, two, th;
  for (int i = 0; i < m; i++) {
    if (s[i] == 1)
      one.insert(c[i]);
    else if (s[i] == 2)
      two.insert(c[i]);
    else if (s[i] == 3)
      th.insert(c[i]);
  }
  if (one.size() >= 2 || two.size() >= 2 || th.size() >= 2) {
    cout << -1 << endl;
    return 0;
  }
  int ans = 0;
  if (n == 3) {
    if (one.size() && *one.begin() != 0)
      ans += 100 * (*one.begin());
    else if (*one.begin() == 0) {
      cout << -1 << endl;
      return 0;
    } else
      ans += 100;
    if (two.size()) ans += 10 * (*two.begin());
    if (th.size()) ans += (*th.begin());
  } else if (n == 2) {
    if (one.size() && *one.begin() != 0)
      ans += 10 * (*one.begin());
    else if (*one.begin() == 0) {
      cout << -1 << endl;
      return 0;
    } else
      ans += 10;
    if (two.size()) ans += (*two.begin());
  } else if (n == 1) {
    if (one.size() && *one.begin() != 0)
      ans += (*one.begin());
    else if (*one.begin() == 0) {
      cout << -1 << endl;
      return 0;
    } else
      ans = 1;
  }
  cout << ans << endl;
  return 0;
}
