#include <bits/stdc++.h>

using namespace std;

struct Node {
  vector<int> outgoing = vector<int>();
  vector<int> incoming = vector<int>();
};

void dfs_forward(int x, vector<Node> &g, vector<bool> &visited, vector<int> &postorder) {
	stack<pair<int,bool>> S = stack<pair<int,bool>>();
	S.push(make_pair(x,false));
	while(not S.empty()){
		auto para = S.top();
		S.pop();
		x = para.first;
		if(para.second){
			postorder.push_back(x);
		}
        else{
			if(not visited[x]){
				visited[x] = true;
				S.push(make_pair(x, true));    
				for(int i = 0; i < g[x].outgoing.size(); i++){
					if(not visited[g[x].outgoing[i]]){
						S.push(make_pair(g[x].outgoing[i], false));
					}
				}
			}
		}
	}
}

void dfs_backward(int x, vector<Node> &g, vector<bool> &visited, vector<vector<int>> &components){
	stack<int> S = stack<int>();
	S.push(x);
	components.push_back(vector<int>());
	while(not S.empty()){
		x = S.top();
		S.pop();
		if(!visited[x]){
			visited[x] = true;
					components[components.size()-1].push_back(x);
		for(int i = 0; i < g[x].incoming.size(); i++){
			if(not visited[g[x].incoming[i]]){
				S.push(g[x].incoming[i]);
				}
			}
		}
	}
}



vector <vector<int>> Kosaraju(vector<Node> &g, int n){
	vector<bool> visited = vector<bool>(n);
	vector<int> postorder = vector<int>();
	vector <vector<int>> components = vector<vector<int>>();
	for (int i = 0; i < n; i++){
		if (!visited[i]){
			dfs_forward(i,g,visited,postorder);
		}
	}
	for (int i = 0; i < n; i++){
		visited[i] = false;
	}
	int component_id = 0;
	reverse(postorder.begin(), postorder.end());
	for(int i = 0; i < n; i++){
		int j = postorder[i];
		if(!visited[j]){
			dfs_backward(j,g,visited,components);
		}
	}
	return components;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int n, m;
	cin >> n >> m;
	vector<Node> g(n);


	int a, b;
	while (m--) {
		cin >> a >> b;
		g[a].outgoing.push_back(b);
		g[b].incoming.push_back(a);
	}

	vector <vector<int>> A = Kosaraju(g,n);

	cout<<A.size()<<'\n';
	for(int i = 0; i < A.size(); i++){
		sort(A[i].begin(),A[i].end(),greater<int>());
	}
	sort(A.begin(),A.end(),[](const std::vector<int>& a, const std::vector<int>& b) {
		return a[0] > b[0];
	});
	
	for(int i = 0; i < A.size(); i++){
		for(int j = 0; j < A[i].size(); j++){
			cout<<A[i][j]<<' ';
		}
		cout<<'\n';
	}
	return 0;
}