import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Scanner;

/**
 * 
 */

/**
 * Dueling Philosophers Problem
 * 
 * Sample solution by Steven Zeil
 * 
 * @author zeil
 *
 * patched by john brodie in order to accomodate SER input and output
 * specifications.
 */
public class DuelingPhilosophers_zeil_SERpatchedBy_jbrodie {
	
	
	Scanner input;
	
	public class Vertex {
		public int inDegree;
		LinkedList<Integer> outgoing;
		
		public Vertex()
		{
			inDegree = 0;
			outgoing = new LinkedList<Integer>();
		}
	}
	
	int N;
	Vertex[] vertex; 
	
	
   public DuelingPhilosophers_zeil_SERpatchedBy_jbrodie(Scanner in, int n, int m /*jbb*/) {
		input = in;
		N = n;
		vertex = new Vertex[N];
		for (int i = 0; i < N; ++i)
			vertex[i] = new Vertex();
		/*jbb while (true) {*/
                for(int i = 0; i < m; ++i) {
			int source = input.nextInt()-1/*jbb*/;
			int dest = input.nextInt()-1/*jbb*/;
			if (source < 0) break;
			++(vertex[dest].inDegree);
			vertex[source].outgoing.add(dest);
		}
	}
	
	
	public void solve()
	{
		// Fill the queue with all vertices with in-degree 0
		LinkedList<Integer> q = new LinkedList<Integer>();
		for (int i = 0; i < N; ++i) {
			if (vertex[i].inDegree == 0)
				q.add(i);
		}

		boolean multiple = false;
		int numProcessed = 0;
		// Successively remove vertices of in-degree 2, decrementing the in-degrees
		// of adjacent vertices as you do, adding any that reach in-degree 0 to the queue.
		while (!q.isEmpty()) {
			if (q.size() > 1)
				multiple = true;
			int v = q.getFirst();
			q.removeFirst();
			++numProcessed;
			for (int w: vertex[v].outgoing) {
				--(vertex[w].inDegree);
				if (vertex[w].inDegree == 0)
					q.add(w);
			}
		}
		if (numProcessed < N)
                   System.out.println ("0"/*jbb"No arrangement is possible"*/);
		else if (multiple)
                   System.out.println ("2"/*jbb"Multiple arrangements are possible"*/);
		else
                   System.out.println ("1"/*jbb"Exactly one arrangement is possible"*/);
	}
	
	
	

	final static void run (BufferedReader bin)
	{
		Scanner in = new Scanner(bin);
		int N = in.nextInt();
		int M = in.nextInt();
		while (N > 0 && M > 0 /*jbb&& in.hasNextDouble()*/)
		{
                   DuelingPhilosophers_zeil_SERpatchedBy_jbrodie problem =
                      new DuelingPhilosophers_zeil_SERpatchedBy_jbrodie(in, N, M);
			problem.solve();
			N = in.nextInt();
			M = in.nextInt();
		}
	}


	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main (String[] argv) throws IOException
	{
		BufferedReader in;
		if (argv.length > 0)
		{
			in = new BufferedReader(new FileReader(argv[0]));
		}
		else {
			in = new BufferedReader (new InputStreamReader(System.in));
		}
		run (in);
	}

}
