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