#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N;
  cin >> M;
  int s[M], c[M];
  for (int i = 0; i < M; i++) {
    cin >> s[i] >> c[i];
  }
  int ans = 0;
  for (int i = 0; i < M; i++) {
    if (s[i] == 1 && c[i] == 0) {
      ans = -1;
      break;
    }
    for (int j = i + 1; j < M; j++) {
      if (s[i] == s[j] && c[i] != c[j]) {
        ans = -1;
        break;
      }
    }
  }
  int candidate[3];
  for (int i = 0; i < 3; i++) candidate[i] = 10;
  if (ans != -1) {
    int digi = 0;
    for (int i = 0; i < M; i++) {
      digi = s[i] - 1;
      if (candidate[digi] > c[i]) {
        candidate[digi] = c[i];
      }
    }
    for (int i = 0; i < 3; i++) {
      if (candidate[i] >= 10) {
        if (i == 0) ans += 100;
      } else {
        ans += candidate[i] * pow(10, 2 - i);
      }
    }
  }
  cout << ans;
}
