#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repr(i,n) for(int i = (int)(n); i >= 0; i--)
#define all(v) v.begin(),v.end()
typedef long long ll;

int main(){
    int N;
    cin >> N;
    int M;
    cin >> M;
    if (M == 0){
        if (N == 1) cout << 0 << endl;
        else if (N == 2) cout << 10 << endl;
        else if (N == 3) cout << 100 << endl;
    }
    else{
        vector<pair<int,int> > vec(M);
        rep(i,M){
            cin >> vec[i].first;
            cin >> vec[i].second;
        }
        bool num = true;
        sort(all(vec));
        int ans = 0;
        if (vec[0].first == 1 && vec[0].second == 0){
            if (N == 1){
                num = true;
            }
            else{
                num = false;
            }
        }
        rep(i,M - 1){
            if (vec[i].first == vec[i + 1].first && vec[i].second != vec[i + 1].second){
                num = false;
            }
        }
        if (!num){
            cout << -1 << endl;
        }
        else{
            if (N == 1){
                ans += vec[0].second;
            }
            else if (N == 2){
                int left = 0;
                int check = 0;
                while(left < M){
                    if (vec[left].first == 1){
                        if (check < 1){
                            ans += 10 * vec[left].second;
                            left++;
                            check = 1;
                        }
                        else{
                            left++;
                        }
                    }
                    else if (vec[left].first == 2){
                        if (check < 2){
                            ans += vec[left].second;
                            left++;
                            check = 2;
                        }
                        else{
                            left++;
                        }
                    }
                    if (ans < 10){
                        ans += 10;
                    }
                }
            }
            else if (N == 3){
                int left = 0;
                int check = 0;
                while(left < M){
                    if (vec[left].first == 1){
                        if (check < 1){
                            ans += 100 * vec[left].second;
                            left++;
                            check = 1;
                        }
                        else{
                            left++;
                        }
                    }
                    else if (vec[left].first == 2){
                        if (check < 2){
                            ans += 10 * vec[left].second;
                            left++;
                            check = 2;
                        }
                        else{
                            left++;
                        }
                    }
                    else if (vec[left].first == 3){
                        if (check < 3){
                            ans += vec[left].second;
                            left++;
                            check = 3;
                        }
                        else{
                            left++;
                        }
                    }
                }
                if (ans < 100){
                    ans += 100;
                }
            }
            cout << ans << endl;
        }
    }
}

Measured memory_bytes: 256