#include <bits/stdc++.h>
using namespace std;
const long long int INF = (long long int)(1e18) + 1;
const int INF_INT = 2147483647;
const long long int mod = 1000000007ll;
int N, M;
int s[5], c[5];
int ans = 0;
void solve() {
  vector<int> digit(3, -1);
  for (int i = 0; i < M; i++) {
    if (digit[s[i] - 1] == -1) {
      digit[s[i] - 1] = c[i];
    } else {
      if (digit[s[i] - 1] != c[i]) {
        ans = -1;
        return;
      }
    }
  }
  if (digit[0] == 0) {
    ans = -1;
    return;
  }
  if (N == 3) {
    if (digit[0] != -1)
      ans += digit[0] * 100;
    else
      ans += 100;
    if (digit[1] != -1) ans += digit[1] * 10;
    if (digit[2] != -1) ans += digit[2];
  }
  if (N == 2) {
    if (digit[0] != -1)
      ans += digit[0] * 10;
    else
      ans += 10;
    if (digit[1] != -1) ans += digit[1];
  }
  if (N == 1) {
    if (digit[0] != -1) ans += digit[0];
  }
}
int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) cin >> s[i] >> c[i];
  solve();
  cout << ans << endl;
  return 0;
}
