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