#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int M;
  cin >> M;
  vector<pair<int, int> > vec(M);
  for (int i = 0; i < (int)(M); i++) {
    cin >> vec[i].first;
    cin >> vec[i].second;
  }
  bool num = true;
  sort(vec.begin(), vec.end());
  int ans = 0;
  if (vec[0].first == 1 && vec[0].second == 0) {
    if (N == 1) {
      num = true;
    } else {
      num = false;
    }
  }
  for (int i = 0; i < (int)(M - 1); i++) {
    if (vec[i].first == vec[i + 1].first &&
        vec[i].second != vec[i + 1].second) {
      num = false;
    }
  }
  if (!num) {
    cout << -1 << endl;
  } else {
    if (N == 1) {
      ans += vec[0].second;
    } else if (N == 2) {
      int left = 0;
      int check = 0;
      while (left < M) {
        if (vec[left].first == 1) {
          if (check < 1) {
            ans += 10 * vec[left].second;
            left++;
            check = 1;
          } else {
            left++;
          }
        } else if (vec[left].first == 2) {
          if (check < 2) {
            ans += vec[left].second;
            left++;
            check = 2;
          } else {
            left++;
          }
        }
      }
    } else if (N == 3) {
      int left = 0;
      int check = 0;
      while (left < M) {
        if (vec[left].first == 1) {
          if (check < 1) {
            ans += 100 * vec[left].second;
            left++;
            check = 1;
          } else {
            left++;
          }
        } else if (vec[left].first == 2) {
          if (check < 2) {
            ans += 10 * vec[left].second;
            left++;
            check = 2;
          } else {
            left++;
          }
        } else if (vec[left].first == 3) {
          if (check < 3) {
            ans += vec[left].second;
            left++;
            check = 3;
          } else {
            left++;
          }
        }
      }
    }
    cout << ans << endl;
  }
}
