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