/*
   Seckin Can Sahin
   bu soruda bir graftaki strongly connected componentleri(kisaca scc) bulmamiz isteniyor.
   cift dfs kullanan ve topolojik siralamadan faydalanan klasik algoritmayi kullandim
   seckincansahin@hotmail.com
*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int n,m,now=0,visited[5001],rev_visited[5001],which_city[10001],finish_time[5001],scc_num=0;
FILE *in,*out;

struct road{
   int to;	
	struct road *next;
}*adj_list[5001],*rev_adj_list[5001],*z,*ptr,*new_road;

int initialize(){
   int i,j;	
	z=(struct road *)malloc(sizeof(struct road));
	z->next=z;
	for(i=0;i<5001;i++){
	   adj_list[i]=(struct road *)malloc(sizeof(struct road));
		adj_list[i]->next=z;
		ptr=(struct road *)malloc(sizeof(struct road));
	   ptr->to=i;
	   ptr->next=adj_list[i]->next;
	   adj_list[i]->next=ptr;
		
		rev_adj_list[i]=(struct road *)malloc(sizeof(struct road));
		rev_adj_list[i]->next=z;	
		ptr=(struct road *)malloc(sizeof(struct road));
	   ptr->to=i;
	   ptr->next=rev_adj_list[i]->next;
	   rev_adj_list[i]->next=ptr;
		
	   finish_time[i]=-1;
	   visited[i]=0;
	   rev_visited[i]=0;
	}
	for(i=0;i<10001;i++)
	   which_city[i]=-1;
	return 0;
}

int read(){
	int i,from,to;
   in=fopen("internet.gir","r");
   out=fopen("internet.cik","w");
	fscanf(in,"%d %d",&n,&m);
   for(i=0;i<m;i++){
	   fscanf(in,"%d %d",&from,&to);
		// add to normal adjacency list	
	   ptr=(struct road *)malloc(sizeof(struct road));
	   ptr->to=to;
	   ptr->next=adj_list[from]->next;
	   adj_list[from]->next=ptr;
	   
		// add to reversed adjacency list
	   ptr=(struct road *)malloc(sizeof(struct road));
	   ptr->to=from;
	   ptr->next=rev_adj_list[to]->next;
	   rev_adj_list[to]->next=ptr;
	}
	return 0;
}
int top_sort(int x){
	int i,j;
	struct road *temp;
	temp=(struct road *)malloc(sizeof(struct road));
	visited[x]=1;
	temp=adj_list[x];
	now++;
	while(temp->next!=z){
		if(visited[temp->next->to]==0){
		   visited[temp->next->to]=1;
	      top_sort(temp->next->to);
		}
	   temp=temp->next;
	}
	now++;
	finish_time[x]=now;
	which_city[now]=x;
	return 0;
}

int print_scc(int x){
	int i,j;
	struct road *temp;
	temp=(struct road *)malloc(sizeof(struct road));
	temp=rev_adj_list[x];
	while(temp->next!=z){
		if(rev_visited[temp->next->to]==0){
			rev_visited[temp->next->to]=1;
	      print_scc(temp->next->to);
		}
		temp=temp->next;
	}
	if(x!=0)
	fprintf(out,"%d ",x);
	return 0;
}

int print(){
   int time;
	for(time=now;time>0;time--){
	   if(which_city[time]!=-1 && !rev_visited[which_city[time]]){
		   rev_visited[which_city[time]]=1;
			print_scc(which_city[time]); 	 
			fprintf(out,"\n");
		}	
	}	
	return 0;
}

int count_scc(int x){
	int i,j;
	struct road *temp;
	temp=(struct road *)malloc(sizeof(struct road));
	temp=rev_adj_list[x];
	while(temp->next!=z){
		if(rev_visited[temp->next->to]==0){
			rev_visited[temp->next->to]=1;
	      count_scc(temp->next->to);
		}
		temp=temp->next;
	}
	return 0;
}
int count(){
   int time;
	for(time=now;time>0;time--){
	   if(which_city[time]!=-1 && !rev_visited[which_city[time]]){
		   rev_visited[which_city[time]]=1;
			count_scc(which_city[time]); 	 
		   if(which_city[time]!=0)
			scc_num++;	
		}	
	}	
	return 0;
}

int main(){
   int i=0,j;
   
   initialize();
   read();
   
   while(adj_list[i]->next->next==z) i++;
   top_sort(i);
   
   for(j=i+1;j<=n;j++)
      if(!visited[j])
		   top_sort(j);
		   
   count();
   fprintf(out,"%d\n",scc_num);
   
   for(i=0;i<5001;i++) rev_visited[i]=0;
   
	print();
	
	fclose(out);
   return 0;   
}
