#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {

	int n, m; cin >> n >> m;
	string ans = "";
	for (int i = 0; i < n; i++) {
		ans += 'x';
	}
	for (int i = 0; i < m; i++) {
		int s; char c; cin >> s >> c;
		s--;
		if (ans[s] != 'x') {
			if (ans[s] == c) {}
			else { // ans[s] != c
				cout << -1 << endl;
				return 0;
			}
		}
		else { // ans[s] == 'x'
			ans[s] = c;
		}
	}
	for (int i = 0; i < n; i++) {
		if (i != 0 && ans[i] == 'x') ans[i] = '0';
		else if (i == 0 && ans[i] == 'x' && ans.size() != 1) ans[i] = '1';
		else if (i == 0 && ans[i] == 'x' && ans.size() == 1) ans[i] = '0';
	}
	if (ans[0] == '0' && 2 <= ans.size()) ans = "-1";
	cout << ans << endl;
    return 0;
}
#include <bits/stdc++.h>
#define MAX 1010
using namespace std;

vector<int> adj[MAX];
int visited[MAX];
int graph[MAX][MAX];

vector<int> dfs(int v, int root)
{
	vector<int> ans;
	visited[v] = 1;
	if (v != root)
	{
		for (int i : adj[v])
		{
			if (i != root && visited[i]) return ans;
		}
		if (graph[v][root])
		{
			ans.push_back(v);
			return ans;
		}
	}
	for (int i : adj[v])
	{
		vector<int> res = dfs(i, root);
		if (!res.empty())
		{
			int flag = 1;
			for (int j : res)
			{
				if (graph[v][j] && i != j)
				{
					flag = 0;
					break;
				}
			}
			if (!flag) continue;
			res.push_back(v);
			return res;
		}
	}
	visited[v] = 0;
	return ans;
}

int main(void)
{
	int N, M;
	cin >> N >> M;
	for (int i = 0; i < M; i++)
	{
		int u, v;
		cin >> u >> v;
		u--;
		v--;
		adj[u].push_back(v);
		graph[u][v] = 1;
	}
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++) visited[j] = 0;
		vector<int> res = dfs(i, i);
		reverse(res.begin(), res.end());
		if (!res.empty())
		{
			cout << res.size() << endl;
			for (int j : res) cout << j + 1 << endl;
			return 0;
		}
	}
	cout << -1 << endl;
	return 0;
}
