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