#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> s(M), c(M);
  vector<int> ck(3);
  for (int i = 0; i < M; i++) {
    cin >> s.at(i) >> c.at(i);
  }
  int ans = 0;
  for (int i = 0; i < M; i++) {
    if (4 - s.at(i) == N && N != 1) {
      if (c.at(i) == 0) {
        ans = -1;
        break;
      }
    }
    for (int j = 0; j < M; j++) {
      if (i == j) continue;
      if (s.at(i) == s.at(j) && c.at(i) != c.at(j)) {
        ans = -1;
        break;
      }
    }
    if (ans < 0) break;
    if (ck.at(s.at(i) - 1) == 1) continue;
    ans += pow(10, 3 - s.at(i)) * c.at(i);
    ck.at(s.at(i) - 1)++;
  }
  cout << ans << endl;
  return 0;
}
