#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int s, c;
  pair<int, int> p[10];
  int ma = 4;
  for (int i = 1; i <= m; i++) {
    cin >> s >> c;
    if (ma > s) ma = s;
    p[i] = make_pair(s, c);
  }
  sort(p, p + m);
  int a = 0;
  for (int i = 1; i < m; i++) {
    for (int j = i + 1; j <= m; j++) {
      if (p[i].first == p[j].first && p[i].second != p[j].second) {
        cout << "-1" << endl;
        return 0;
      } else if (p[i].first == p[j].first && p[i].second == p[j].second) {
        p[i].second = 0;
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    if (p[i].first == 1) a += 100 * p[i].second;
    if (p[i].first == 2) a += 10 * p[i].second;
    if (p[i].first == 3) a += p[i].second;
  }
  if (ma == 3)
    ma = 1;
  else if (ma == 1)
    ma = 3;
  if (n != ma) {
    cout << "-1" << endl;
    return 0;
  }
  cout << a << endl;
  return 0;
}
