#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto& a : x)
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) beg(x), en(x)
#define resz resize
const int MOD = 1000000007; // 998244353
const ll INF = 1e18;
const int MX = 1000001;
const ld PI = 4*atan((ld)1);
template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }
namespace io {
// TYPE ID (StackOverflow)
template<class T> struct like_array : is_array<T>{};
template<class T, size_t N> struct like_array<array<T,N>> : true_type{};
template<class T> struct like_array<vector<T>> : true_type{};
template<class T> bool is_like_array(const T& a) { return like_array<T>::value; }
// I/O
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void setIO(string s = "") {
ios_base::sync_with_stdio(0); cin.tie(0);
if (sz(s)) { setIn(s+".in"), setOut(s+".out"); }
}
// INPUT
template<class T> void re(T& x) { cin >> x; }
template<class Arg, class... Args> void re(Arg& first, Args&... rest);
void re(double& x) { string t; re(t); x = stod(t); }
void re(ld& x) { string t; re(t); x = stold(t); }
template<class T> void re(complex<T>& x);
template<class T1, class T2> void re(pair<T1,T2>& p);
template<class T> void re(vector<T>& a);
template<class T, size_t SZ> void re(array<T,SZ>& a);
template<class Arg, class... Args> void re(Arg& first, Args&... rest) { re(first); re(rest...); }
template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); }
template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
// OUTPUT
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) {
os << '{' << a.f << ", " << a.s << '}'; return os;
}
template<class T> ostream& printArray(ostream& os, const T& a, int SZ) {
os << '{';
F0R(i,SZ) {
if (i) {
os << ", ";
if (is_like_array(a[i])) cout << "\n";
}
os << a[i];
}
os << '}';
return os;
}
template<class T, size_t SZ> ostream& operator<<(ostream& os, const array<T,SZ>& a) {
return printArray(os,a,SZ);
}
template<class T> ostream& operator<<(ostream& os, const vector<T>& a) {
return printArray(os,a,sz(a));
}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {
os << vector<T>(all(a)); return os;
}
template<class T1, class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {
os << vector<pair<T1,T2>>(all(a)); return os;
}
template<class T> void pr(const T& x) { cout << x << '\n'; }
template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) {
cout << first << ' '; pr(rest...);
}
}
using namespace io;
namespace modOp {
int ad(int a, int b, int mod = MOD) { return (a+b)%mod; }
int sub(int a, int b, int mod = MOD) { return (a-b+mod)%mod; }
int mul(int a, int b, int mod = MOD) { return (ll)a*b%mod; }
int AD(int& a, int b, int mod = MOD) { return a = ad(a,b,mod); }
int SUB(int& a, int b, int mod = MOD) { return a = sub(a,b,mod); }
int MUL(int& a, int b, int mod = MOD) { return a = mul(a,b,mod); }
int po (int b, int p, int mod = MOD) { return !p?1:mul(po(mul(b,b,mod),p/2,mod),p&1?b:1,mod); }
int inv (int b, int mod = MOD) { return po(b,mod-2,mod); }
int invGeneral(int a, int b) { // 0 < a < b, gcd(a,b) = 1
if (a == 0) return b == 1 ? 0 : -1;
int x = invGeneral(b%a,a);
return x == -1 ? -1 : ((1-(ll)b*x)/a+b)%b;
}
}
using namespace modOp;
int N,M;
vpi dist[MX];
int main() {
// you should actually read the stuff at the bottom
setIO(); re(N,M);
pi en;
F0R(i,M) {
int a,b; re(a,b);
if (i == 0) dist[a].pb({0,-1});
if (sz(dist[b]) == 0 || dist[a].back().f+1 < dist[b].back().f)
dist[b].pb({dist[a].back().f+1,a});
if (i == M-1) en = {dist[b].back().f,b};
}
FOR(i,1,N+1) reverse(all(dist[i]));
vi ans;
while (1) {
ans.pb(en.s);
if (en.f == 0) break;
en = dist[en.s][lb(all(dist[en.s]),mp(en.f,-MOD))-dist[en.s].begin()];
en.f --;
}
reverse(all(ans));
pr(sz(ans)-1);
F0R(i,sz(ans)-1) pr(ans[i],ans[i+1]);
// you should actually read the stuff at the bottom
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?), set tle
* do smth instead of nothing and stay organized
*/
I3ByYWdtYSBHQ0Mgb3B0aW1pemUgKCJPMyIpCiNwcmFnbWEgR0NDIHRhcmdldCAoInNzZTQiKQoKI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDxleHQvcGJfZHMvdHJlZV9wb2xpY3kuaHBwPgojaW5jbHVkZSA8ZXh0L3BiX2RzL2Fzc29jX2NvbnRhaW5lci5ocHA+CiNpbmNsdWRlIDxleHQvcm9wZT4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnVzaW5nIG5hbWVzcGFjZSBfX2dudV9wYmRzOwp1c2luZyBuYW1lc3BhY2UgX19nbnVfY3h4OwogCnR5cGVkZWYgbG9uZyBsb25nIGxsOwp0eXBlZG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