#include <bits/stdc++.h>
using namespace std;
int N3(int m, vector<int> &x, vector<int> s, vector<int> c) {
  for (int i = 0; i < m; i++) {
    if (s[i] == 1)
      x[0] = 100 * c[i];
    else if (s[i] = 2)
      x[1] = 10 * c[i];
    else
      x[2] = c[i];
  }
}
int N2(int m, vector<int> &x, vector<int> s, vector<int> c) {
  for (int i = 0; i < m; i++) {
    if (s.at(i) == 1)
      x[0] = 10 * c.at(i);
    else
      x[1] = c.at(i);
  }
}
int N1(int m, vector<int> &x, vector<int> s, vector<int> c) {
  for (int i = 0; i < m; i++) {
    x[0] = c.at(i);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> s(m);
  vector<int> c(m);
  for (int i = 0; i < m; i++) {
    cin >> s.at(i) >> c.at(i);
    if (((s.at(i) == 2) || (s.at(i) == 3)) && c.at(i) == 0) {
      cout << -1 << endl;
      exit(0);
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = i + 1; j < m; m++) {
      if ((s.at(i) == s.at(j)) && (c.at(i) != c.at(j))) {
        cout << -1 << endl;
        exit(0);
      }
    }
  }
  int sum = -1;
  vector<int> x(n, 0);
  if (n == 3) {
    N3(m, x, s, c);
    sum = x[0] + x[1] + x[2];
  } else if (n == 2) {
    N2(m, x, s, c);
    sum = x[0] + x[1];
  } else {
    N1(m, x, s, c);
    sum = x[0];
  }
  cout << sum << endl;
}
