#include <bits/stdc++.h>
using namespace std;
using ll = long long;
bool is_int_lround(double x) { return std::lround(x) == x; }
int ketasuu(int x) {
  int n = 0;
  while (x > 0) {
    x /= 10;
    n++;
  }
  return n;
}
ll gcd11(ll x, ll y) {
  long long a = max(x, y), b = min(x, y), c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}
int abs(int x, int y) {
  if (x >= y) {
    return x - y;
  } else if (y > x) {
    return y - x;
  }
}
int fiv(int n) {
  if (n == 0) {
    return 0;
  } else if (n == 1) {
    return 1;
  }
  return fiv(n - 1) + fiv(n - 2);
}
ll conbi(int n, int m) {
  cin >> n >> m;
  vector<ll> a(100);
  a[0] = 1;
  for (int i = 0; i < 14; i++) {
    a[i + 1] = a[i] * (i + 1);
  }
  return a[n] / (a[m] * a[n - m]);
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> s(m);
  vector<int> c(m);
  ;
  for (int i = 0; i < m; i++) {
    cin >> s[i] >> c[i];
  }
  bool hantei = true;
  int min = 0;
  if (n == 3) {
    ;
    for (int i = 0; i < m; i++) {
      if (s[i] == 1 && c[i] == 0) {
        hantei = false;
      }
    }
    bool hyaku3 = false;
    ;
    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j < m; j++) {
        if (s[i] == s[j] && c[i] != c[j]) {
          hantei = false;
        }
      }
    };
    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j < m; j++) {
        if (s[i] == s[j] && c[i] == c[j]) {
          c[i] = 0;
        }
      }
    };
    for (int i = 0; i < m; i++) {
      if (s[i] == 1) {
        hyaku3 = true;
      }
    }
    if (hyaku3 != true) {
      min += 100;
    }
    if (hantei && m != 0) {
      ;
      for (int i = 0; i < m; i++) {
        if (s[i] == 1) {
          min += c[i] * 100;
        } else if (s[i] == 2) {
          min += c[i] * 10;
        } else {
          min += c[i];
        }
      }
      cout << min << endl;
    } else if (hantei && m == 0) {
      cout << 100 << endl;
    } else {
      cout << -1 << endl;
    }
  }
  if (n == 2) {
    ;
    for (int i = 0; i < m; i++) {
      if (s[i] == 2 && c[i] == 0) {
        hantei = false;
      }
    }
    bool ju2 = false;
    ;
    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j < m; j++) {
        if (s[i] == s[j] && c[i] != c[j]) {
          hantei = false;
        }
      }
    };
    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j < m; j++) {
        if (s[i] == s[j] && c[i] == c[j]) {
          c[i] = 0;
        }
      }
    };
    for (int i = 0; i < m; i++) {
      if (s[i] == 2) {
        ju2 = true;
      }
    }
    if (ju2 != true) {
      min += 10;
    }
    if (hantei && m != 0) {
      ;
      for (int i = 0; i < m; i++) {
        if (s[i] == 2) {
          min += c[i] * 10;
        } else {
          min += c[i];
        }
      }
      cout << min << endl;
    } else if (hantei && m == 0) {
      cout << 10 << endl;
    } else {
      cout << -1 << endl;
    }
  } else if (n == 1) {
    ;
    for (int i = 0; i < m; i++) {
      if (s[i] == 3 && c[i] == 0) {
        hantei = false;
      }
    };
    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j < m; j++) {
        if (s[i] == s[j] && c[i] != c[j]) {
          hantei = false;
        }
      }
    };
    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j < m; j++) {
        if (s[i] == s[j] && c[i] == c[j]) {
          c[i] = 0;
        }
      }
    }
    if (hantei && m != 0) {
      ;
      for (int i = 0; i < m; i++) {
        if (s[i] == 3) {
          min += c[i];
        }
      }
      cout << min << endl;
    } else if (hantei && m == 0) {
      cout << 0 << endl;
    } else {
      cout << -1 << endl;
    }
  }
}
