#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  int s[M], c[M];
  int ans = 0;
  int count1 = 0;
  int count2 = 0;
  int count3 = 0;
  for (int i = 0; i < M; ++i) cin >> s[i] >> c[i];
  for (int j = 0; j < M; ++j) {
    if (s[j] == 1 && c[j] == 0 && N > 1) {
      cout << -1;
      return 0;
    }
    for (int k = 0; k < M; ++k) {
      if (j == k) continue;
      if (s[j] == s[k] && c[j] != c[k]) {
        cout << -1;
        return 0;
      }
    }
    if (s[j] == 1 && count1 == 0) {
      if (N == 1) ans += c[j];
      if (N == 2) ans += c[j] * 10;
      if (N == 3) ans += c[j] * 100;
      count1 = 1;
    }
    if (s[j] == 2 && count2 == 0) {
      if (N == 2) ans += c[j];
      if (N == 3) ans += c[j] * 10;
      count2 = 1;
    }
    if (s[j] == 3 && count3 == 0) {
      if (N == 3) ans += c[j];
      count3 = 1;
    }
  }
  cout << ans;
}
