#include<algorithm>//sort(all(変数),greater<型名>())で降順に、*max_element(all(変数))で最大値を獲得
#include<bits/stdc++.h>
#include<cmath>//切り上げceil(値)
#include<deque>
#include<iomanip>//setprecision(数字)
#include<iostream>
#include<list>
#include<map>
#include<numeric>
// xとyのgcdは__gcd(x,y),xとyのlcmは(x*y)/__gcd、accumulate(all(変数),0)で要素の全加算
#include<queue>//priority_queue<型名> 変数
#include<set>
#include<stack>
#include<string>//変数.substr(どこから,いくつ)
#include<vector>
#define rep(i,n)  for(ll i=0; i<n; i++)
#define rrep(i,n) for(ll i=n-1; 0<=i; i--)
#define vll vector<ll>
#define vi vector<int>
#define all(x) (x).begin(),(x).end()
#define INF 1e9
using ll = long long;
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

int main(){
  int n,m;  cin >> n >> m;
  vector<vi> x(n);
  rep(i,m){
    int s,c; cin >> s >> c;
    s--;
    x[s].push_back(c);
  }
  rep(i,n){
    if(x[i].size() == 0){
      x[i].push_back(0);
    }
  }
  int ans = 0;
  if(n == 1){
    ans += *min_element(all(x[0]));
  }else{
    rep(i,n){
      if(i == 0){
        if(*max_element(all(x[i])) == 0){
          cout << -1;
          return 0;
        }else{
          sort(all(x[i]));
          int j = 0;
          while(x[j] != 0)j++;
          ans += x[j];
        }
      }else{
        ans *= 10;
        ans += *min_element(all(x[i]));
      }
    }
  }
  cout << ans;
  cout << "\n";
  return 0;
}
