// Arup Guha
// 11/18/2012
// Solution to 2012 South East Regional (D2) Problem D: Dueling Philosophers
// Cleaned up on 3/3/2022

import java.util.*;

public class duel_alt {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);

		int n = stdin.nextInt();
		int numE = stdin.nextInt();

		// Process all cases.
		while (n != 0) {

			// Set up ordering matrix.
			ArrayList<Integer>[] graph = new ArrayList[n];
			for (int i=0; i<n; i++)
				graph[i] = new ArrayList<Integer>();

			// Put in edges in both the appropriate in and out degree lists.
			for (int i=0; i<numE; i++) {
				int prev = stdin.nextInt();
				int next = stdin.nextInt();
				graph[prev-1].add(next-1);
			}

			// Run a topological sort.
			System.out.println(topSort(graph));

			// Get next case.
			n = stdin.nextInt();
			numE = stdin.nextInt();
		}
	}

	public static int topSort(ArrayList<Integer>[] graph) {

		// Store in degrees of each vertex.
		int n = graph.length;
		int[] inDegree = new int[n];
		for (int i=0; i<n; i++) {
			for (Integer next: graph[i])
				inDegree[next]++;
		}

		// Stores everything with in degree 0.
		LinkedList<Integer> q = new LinkedList<Integer>();

		// Add everything into the queue that has no pre-requisites.
		for (int i=0; i<n; i++)
			if (inDegree[i] == 0)
				q.offer(i);

		// Store if our queue size ever gets to 2.		
		boolean hasTwo = q.size() > 1;
		
		int numPlaced = 0;

		// Start the top sort.
		while (q.size() > 0) {

			// Get the next item.
			int cur = q.poll();
			numPlaced++;

			// Marking two options.
			if (q.size() > 0) hasTwo = true;

			// Subtract one from each appropriate in degree.
			for (Integer next: graph[cur]) {
				inDegree[next]--;
				if (inDegree[next] == 0)
					q.offer(next);
			}			
		}

		// Here is our answer.
		if (numPlaced < n) return 0;
		return hasTwo ? 2 : 1;			
	}
}

