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