#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main() {
	vector<pair<int, int>> x(100), y(100);
	int n, m, o, t, h;
	o = t = h = 10;
	cin >> n >> m;
	vector<int> s(m), c(m);
	for(int i = 0; i < m; ++i) {
		cin >> s.at(i) >> c.at(i);
		if(n == 1 && c.at(i) == 0) {
			cout << 0 << endl;
			return 0;
		}
		(s.at(i) == 1 ? o = min(o, c.at(i)) : 
			(s.at(i) == 2 ? t = min(t, c.at(i)) : h = min(h, c.at(i))));
		x.at(s.at(i)).first += 1;
		if(x.at(s.at(i)).first > 0 && c.at(i) == x.at(s.at(i)).second) {
			x.at(s.at(i)).first -= 1;
			x.at(s.at(i)).second = c.at(i);
		}
		if(x.at(s.at(i)).second == 0)
			x.at(s.at(i)).second = c.at(i);
		else if(x.at(s.at(i)).second != c.at(i)) {
			cout << "-1" << endl;
			return 0;
		}
		if(x.at(s.at(i)).second == c.at(i))
			x.at(s.at(i)).first = 1;
			cout << "-1" << endl;
			return 0;
		}
		if(x.at(s.at(i)).first != 0)
			if(x.at(s.at(i)).second != c.at(i)) {
				cout << "-1" << endl;
				return 0;
			}
	}
	if(o == 10) {
		o = 1;
		if(n == 1)
			o = 0;
	}
	if(t == 10) {
		t = 0;
	}
	if(h == 10)
		h = 0;

	cout << o;
	if(n == 2)
		cout << t;
	if(n == 3)
		cout << t << h;
	cout << endl;

}
