#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> z(N);
  vector<int> s(M + 5);
  vector<int> c(M + 5);
  for (int i = 0; i < M; i++) cin >> s[i] >> c[i];
  for (int i = 0; i < N; i++) {
    z[i] = 100;
  }
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < M; j++) {
      if (s[i] == s[j] && z[s[i]] > c[j] && i != j) z[s[i]] = c[j];
    }
  }
  for (int i = 0; i < N; i++) {
    if (z[i] == 100) {
      cout << -1;
      return 0;
    }
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    ans += pow((double)z[i], s[i]);
  }
  cout << ans;
  return 0;
}
