use std::collections::HashMap;
fn read<T: std::str::FromStr>() -> T {
	let mut s = String::new();
	std::io::stdin().read_line(&mut s).ok();
	s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
	read::<String>().split_whitespace()
		.map(|e| e.parse().ok().unwrap()).collect()
}

fn read_vec2<T: std::str::FromStr>(n: u32) -> Vec<Vec<T>> {
	(0..n).map(|_| read_vec()).collect()
}

pub trait LexicalPermutation {
	/// Return `true` if the slice was permuted, `false` if it is already
	/// at the last ordered permutation.
	fn next_permutation(&mut self) -> bool;
}
impl<T> LexicalPermutation for [T] where T: Ord {
	/// Original author in Rust: Thomas Backman <serenity@exscape.org>
	fn next_permutation(&mut self) -> bool {
		// These cases only have 1 permutation each, so we can't do anything.
		if self.len() < 2 { return false; }

		// Step 1: Identify the longest, rightmost weakly decreasing part of the vector
		let mut i = self.len() - 1;
		while i > 0 && self[i-1] >= self[i] {
			i -= 1;
		}

		// If that is the entire vector, this is the last-ordered permutation.
		if i == 0 {
			return false;
		}

		// Step 2: Find the rightmost element larger than the pivot (i-1)
		let mut j = self.len() - 1;
		while j >= i && self[j] <= self[i-1]  {
			j -= 1;
		}

		// Step 3: Swap that element with the pivot
		self.swap(j, i-1);

		// Step 4: Reverse the (previously) weakly decreasing part
		self[i..].reverse();

		true
	}
}

fn main() {
	let (n, m) = {
		let tmp: Vec<usize> = read_vec();
		(tmp[0], tmp[1])
	};

	let mut s: Vec<usize> = Vec::new();
	let mut c: Vec<usize> = Vec::new();

	let mut hash = [(1, Vec::new()), (2, Vec::new()), (3, Vec::new())].iter().cloned().collect::<HashMap<_, _>>();

	for _ in 0..m {
		let mut tmp: Vec<usize> = read_vec();
		let vector = hash.entry(tmp[0]).or_insert(Vec::new());
		if tmp[1] != 0 {
			vector.push(tmp[1]);
		}
	}

	let mut ans: isize = 0;

	let vector_1 = hash.get(&1).unwrap();
	let vector_2 = hash.get(&2).unwrap();
	let vector_3 = hash.get(&3).unwrap();

	let mut vector_1_min = 0;
	if vector_1.len() != 0 {
		vector_1_min = *vector_1.iter().min().unwrap() as isize;
	}

	let mut vector_2_min = 0;
	if vector_2.len() != 0 {
		vector_2_min = *vector_2.iter().min().unwrap() as isize;
	}

	let mut vector_3_min = 0;
	if vector_3.len() != 0 {
		vector_3_min = *vector_3.iter().min().unwrap() as isize;
	}

	if n > m {
		ans = -1;
	} else if n==3 {
		if vector_1_min == 0 {
			ans = -1;
		} else {
			ans = 100*vector_1_min + 10*vector_2_min + vector_3_min;
		}
	} else if n==2 {
		if vector_1_min == 0 {
			ans = -1;
		} else {
			ans = 10*vector_1_min + vector_2_min;
		}
	} else {
		ans = vector_1_min;
	}

	println!("{}", ans);
}
