#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const long long LINF = 1000000000000000000;
const double EPS = 1e-10;
int main(void) {
  int N, M;
  cin >> N >> M;
  vector<int> index(M);
  vector<int> value(M);
  for (int i = 0; i < M; i++) {
    cin >> index[i] >> value[i];
    if (N == 3 || N == 2) {
      if (index[i] == 1 && value[i] == 0) {
        printf("-1\n");
        return (0);
      }
    }
  }
  if (N == 1) {
    sort(value.begin(), value.end());
    printf("%d\n", value[0]);
  } else if (N == 2) {
    int min1 = INF;
    int min2 = INF;
    bool found1 = false;
    bool found2 = false;
    for (int i = 0; i < M; i++) {
      if (index[i] == 1) {
        min1 = min(min1, value[i]);
        found1 = true;
      } else if (index[i] == 2) {
        min2 = min(min2, value[i]);
        found2 = true;
      }
    }
    if (found1 == false && found2 == true) {
      printf("%d\n", 10 + min2);
    } else if (found1 == true && found2 == false) {
      printf("%d\n", min1 * 10);
    } else if (found1 == true && found2 == true) {
      printf("%d\n", min1 * 10 + min2);
    }
  } else if (N == 3) {
    int min1 = INF;
    int min2 = INF;
    int min3 = INF;
    bool found1 = false;
    bool found2 = false;
    bool found3 = false;
    for (int i = 0; i < M; i++) {
      if (index[i] == 1) {
        min1 = min(min1, value[i]);
        found1 = true;
      } else if (index[i] == 2) {
        min2 = min(min2, value[i]);
        found2 = true;
      } else if (index[i] == 3) {
        min3 = min(min3, value[i]);
        found3 = true;
      }
    }
    if (found1 == true && found2 == true && found3 == true) {
      printf("%d\n", min1 * 100 + min2 * 10 + min3);
    } else if (found1 == true && found2 == true && found3 == false) {
      printf("%d\n", min1 * 100 + min2 * 10);
    } else if (found1 == true && found2 == false && found3 == true) {
      printf("%d\n", min1 * 100 + min3);
    } else if (found1 == true && found2 == false && found3 == false) {
      printf("%d\n", min1 * 100);
    } else if (found1 == false && found2 == true && found3 == false) {
      printf("-1\n");
    } else if (found1 == false && found2 == true && found3 == true) {
      printf("%d\n", 100 + min2 * 10 + min3);
    } else if (found1 == false && found2 == false && found3 == true) {
      printf("-1\n");
    }
  }
  return (0);
}
