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