#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  int S[10] = {};
  int C[10] = {};
  for (int i = 0; i < M; i++) {
    cin >> S[i] >> C[i];
  }
  if (N < M) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 0; i < M; i++) {
    if (S[i] == 1) {
      if (C[i] == 0) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  for (int i = 0; i < M; i++) {
    for (int j = 1; j < M; j++) {
      if (S[i] == S[j]) {
        if (C[i] != C[j]) {
          cout << -1 << endl;
          return 0;
        }
      }
    }
  }
  int Ans = 0;
  bool keta3 = false;
  bool keta2 = false;
  bool keta1 = false;
  for (int i = 0; i < M; i++) {
    switch (S[i]) {
      case 1:
        if (!keta3) {
          Ans += (C[i] * 100);
          keta3 = true;
        }
        break;
      case 2:
        if (!keta2) {
          Ans += (C[i] * 10);
          keta2 = true;
        }
        break;
      case 3:
        if (!keta1) {
          Ans += (C[i]);
          keta1 = true;
        }
        break;
    }
  }
  cout << Ans << endl;
  return 0;
}
