#include <bits/stdc++.h>
namespace Mylib {
using namespace std;
int Double(int x, int y) {
  for (int i = 1; i < y; i++) x *= 10;
  return x;
}
}  // namespace Mylib
int main(void) {
  using namespace std;
  int N, M;
  vector<int> vInt;
  int tempInt;
  cin >> N >> M;
  bool flag = true;
  for (int i = 0; i < M; i++) {
    cin >> tempInt;
    vInt.push_back(tempInt);
    cin >> tempInt;
    vInt.push_back(tempInt);
  }
  int digit[N + 1];
  for (int i = 0; i <= N; i++) digit[i] = 10;
  for (int i = 0; i < M * 2; i += 2) {
    tempInt = vInt[i + 1];
    if (digit[vInt[i]] != tempInt && digit[vInt[i]] != 10) {
      cout << -1 << endl;
      return 0;
    }
    if (digit[vInt[i]] > tempInt) {
      if (vInt[i] != 1 && digit[vInt[i]] == 10) {
        digit[vInt[i]] = tempInt;
      } else if (vInt[i + 1] != 0 && digit[vInt[i]] == 10) {
        digit[vInt[i]] = tempInt;
      } else if (vInt[i] == 1 && digit[vInt[i]] == 10) {
        digit[vInt[i]] = tempInt;
      } else {
        flag = false;
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= N && flag; i++) {
    if (digit[1] == 10 || digit[1] == 0) flag = false;
    if (digit[i] == 10) continue;
    ans += Mylib::Double(digit[i], N + 1 - i);
  }
  if (N == 1 && (digit[1] == 0 || digit[1] == 10)) flag = true;
  if (flag == true) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}
