#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> vec(M, vector<int>(2));
  for (int i = 0; i < (int)(M); i++)
    for (int j = 0; j < (int)(2); j++) cin >> vec[i][j];
  int x;
  if (M == 0) x = 0;
  if (N == 1) {
    int tmp3 = 0;
    for (int i = 0; i < (int)(M); i++) {
      if (vec[i][0] == 1) {
        if (tmp3 == vec[i][1])
          continue;
        else if (tmp3 == 0) {
          x += vec[i][1] * 1;
          tmp3 = vec[i][1];
        } else {
          cout << "-1" << endl;
          return 0;
        }
      }
    }
  }
  if (N == 2) {
    int tmp2 = 0;
    int tmp3 = 0;
    for (int i = 0; i < (int)(M); i++) {
      if (vec[i][0] == 1) {
        if (vec[i][1] == 0) {
          cout << "-1" << endl;
          return 0;
        } else if (tmp2 == vec[i][1])
          continue;
        else if (tmp2 == 0) {
          x += vec[i][1] * 10;
          tmp2 = vec[i][1];
        } else {
          cout << "-1" << endl;
          return 0;
        }
      }
      if (i == M - 1) {
        if (x == 0) {
          cout << "-1" << endl;
          return 0;
        }
      }
    }
    for (int i = 0; i < (int)(M); i++) {
      if (vec[i][0] == 2) {
        if (tmp3 == vec[i][1])
          continue;
        else if (tmp3 == 0) {
          x += vec[i][1] * 1;
          tmp3 = vec[i][1];
        } else {
          cout << "-1" << endl;
          return 0;
        }
      }
    }
  }
  if (N == 3) {
    int tmp1 = 0;
    int tmp2 = 0;
    int tmp3 = 0;
    for (int i = 0; i < (int)(M); i++) {
      if (vec[i][0] == 1) {
        if (vec[i][1] == 0) {
          cout << "-1" << endl;
          return 0;
        } else if (tmp1 == vec[i][1])
          continue;
        else if (tmp1 == 0) {
          x += vec[i][1] * 100;
          tmp1 = vec[i][1];
        } else {
          cout << "-1" << endl;
          return 0;
        }
      }
      if (i == M - 1) {
        if (x == 0) {
          cout << "-1" << endl;
          return 0;
        }
      }
    }
    for (int i = 0; i < (int)(M); i++) {
      if (vec[i][0] == 2) {
        if (tmp2 == vec[i][1])
          continue;
        else if (tmp2 == 0) {
          x += vec[i][1] * 10;
          tmp2 = vec[i][1];
        } else {
          cout << "-1" << endl;
          return 0;
        }
      }
    }
    for (int i = 0; i < (int)(M); i++) {
      if (vec[i][0] == 3) {
        if (tmp3 == vec[i][1])
          continue;
        else if (tmp3 == 0) {
          x += vec[i][1] * 1;
          tmp3 = vec[i][1];
        } else {
          cout << "-1" << endl;
          return 0;
        }
      }
    }
  }
  cout << x << endl;
  return 0;
}
