#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> a(m, vector<int>(1));
  for (int i = 0; i < (int)(m); i++) {
    cin >> a[i][0] >> a[i][1];
  }
  sort((a).begin(), (a).end(),
       [](const vector<int> &alpha, const vector<int> &beta) {
         return alpha[0] < beta[0];
       });
  for (int i = 0; i < (int)(m - 1); i++) {
    if (a[i][0] == a[i + 1][0] && a[i][1] != a[i + 1][1]) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int i = 0; i < (int)(m - 1); i++) {
    if (a[i][0] == a[i + 1][0] && a[i][1] == a[i + 1][1]) a[i + 1][1] = 0;
  }
  if (n >= 2) {
    for (int i = 0; i < (int)(m); i++) {
      if (a[i][0] == 1 && a[i][1] == 0) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  if (n == 1) {
    cout << a[0][1] << endl;
  }
  int ans = 0;
  bool nika = true;
  if (n == 2) {
    for (int i = 0; i < (int)(m); i++) {
      if (a[i][0] == 1) {
        ans += a[i][1] * 10;
        nika = false;
      }
      if (a[i][0] == 2) {
        ans += a[i][1];
        nika = false;
      }
    }
    cout << ans << endl;
    return 0;
  }
  if (n == 3) {
    for (int i = 0; i < (int)(m); i++) {
      if (a[i][0] == 1) {
        ans += a[i][1] * 100;
      }
      if (a[i][0] == 2) {
        ans += a[i][1] * 10;
      }
      if (a[i][0] == 3) {
        ans += a[i][1];
      }
    }
    cout << ans << endl;
    return 0;
  }
}
