/*
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNMMMMMNmmmNNMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNNNNMMNNNMMMMMMMNNNNNNNNMMMMNmddNNMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNNNmmNNNNNNNNNNNNNNNNNNNmmmNNNNNNNNdyhmNMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNmmmNmmmmNmmmNmNNmmNNNmmmmmmmmmmmmmmmmddNNNdhmNMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNNNmmmmdddmdhddmmmmmmmmmmmmmmdddddddhhhdhhhhddmNmddNMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNNNmmmmmmdhhhyhddddhhhhhhyyhyyhhhddddmddhhyyyyhhddmmmhhmMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMNNNNNNmmmmddddddyyyyhhyyyoysoooooooooooooosssyhdmmddhhhhdddmmmhhmMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMNNNNNmmddddhdhhhyssssoosoos+/+++++++++++++++ooooossssyyhhhhhhhhddyhmMMMMMMMMM
MMMMMMMMMMMMMMMMMMNMMMMNmmmmmddddhhyys+ooo++++oo+///////////////////+++oooooossyysyysyyyddhmNMMMMMMM
MMMMMMMMMMMMMMMMMNNNNNNNdmmddddhhhy+o////////////::///:::--::::::://///++ooosssyyyysssssyyddmNMMMMMM
MMMMMMMMMMMMMMMMMNNNmmmNddddhhhyss+o/::://:::/::::::-------::::::::::///////+++oossoooossyydmmMMMMMM
MMMMMMMMMMMMMMMNNmNmmddmdhhhyssoo++::/://:::::::::-----------------:::::///////++++oooooosyhmmNMMMMM
MMMMMMMMMMMMMMNmNdmdmdhmhhyysooo+/::/:::::::--:----------------------:::::::::////++++++oosyhmmMMMMM
MMMMMMMMMMMMMNNmNmmdddyhhhsooo++:://--::::::-------------....-----------:::::::://////+++ooshmNNMMMM
MMMMMMMMMMMMNNNmNmddhhhhhyo+o++/:/:----::-------:-----.......------------:::::::////////+oosymNNMMMM
MMMMMMMMMMMMNNNmNmddhyyyyys/+++:/:---------:/::---...........-.--....--------:::::::///+++osydNNMMMM
MMMMMMMMMMMMNNNNNmdddysh+ys//+/:/--------://:--....--...`..--------------------::::::/++++syhhmNMMMM
MMMMMMMMMMMNNNNmmmddhhoo++s///:/:-----:::/:--...-::---::://++///:--:::-----------:::/++ooyyhhdmNNMMM
MMMMMMMMMMMNNNNmmmmdhho+s:s+/::/------:::---...-::/+++/:////::-::---:::-:--------:::oosooshdmmNNMMMM
MMMMMMMMMMMNNNNmmNddhys/+++o/:::-----:::--...-:/+++:-...---:---------------------::oss+oosyhdmMNMMMM
MMMMMMMMMMMNNNNmNmddyys//+/+//:-----::------:++/:-....`....---------------------::+yy++++oosshNNMMMM
MMMMMMMMMMMNNNNNmdmdsys+//+///-----::------/+/:-...``......---------........---::/sy+///+++ossmNMMMM
MMMMMMMMMMMNNNNmmdddyy+++//:::---------:--://:...`....-:///++/:-----........---::soo///++ooosymNMMMM
MMMMMMMMMMMNNNNmmdydsy//o:::------------://:-......:/++/+oyhhyo/:---.........--:oos:/ossyyhhhdmNMMMM
MMMMMMMMMMMNNNNNmhyyos///+::-----------::-......-:////+oymNNmhoo+-.--...````..-:sy//ohmNNhdNmmmMMMMM
MMMMMMMMMMMNMNNNmhs+++//:///:---................:::++:.:NMMMMNdos/.--.``````..-+y+:+-mMMMNhmNmNMMMMM
MMMMMMMMMMMMMMNNmy++//+//:/:/---...............-/so:`` .hyNMMMNyoy---``````...-/d/:-`+NMMMNmmNMMMMMM
MMMMMMMMMNmmmmNNmy+//:/+/::::--.......```.....-////://:.:shdddhsoo:--.........--ss--::oyhdmmmNMMMMMM
MMMMMMMMds+///+osoo+/::/+/::---........```....-.......---::::::/::---........-.::yo---:::/+sdmMMMMMM
MMMMMMMh+///:::::--:///:+/:::---....`..`.``.......``````....--:::-----..--....--::ss:--:://oymNMMMMM
MMMMMMd::-/o:--::+/-.-/o/+:-:---.........`.....`````````....----------........----:oy/-:://+ohmNMMMM
MMMMMMh-:.:y-----:os:-.:o//--:--.........``..````...........--------............---:/yo:::++osdmMMMM
MMMMMMN/-:-y:--://-/y+:.:o//-::-............```````........---------.....---......---:oo:::/ooymNMMM
MMMMMMMd-:-s--//:-.:+so:-/+//-/:...........``````````.........-----...------..`....--:/s+:::/++hmMMM
MMMMMMMMd-/o::/-...-:s+::.///+:+--.........````````````.........---...---..--:-----:/+oso::::/+omNMM
MMMMMMMMMm:/s.++:....:s.-+.//+/:/-........````````````````.....-::-...--:/+/:--://++o+oso:::://odNMM
MMMMMMMMMMm-/:`:s+:---/s.:o-//+//:-........````````````````...-/+-.....---..-:::://+ossso/:::/+odNMM
MMMMMMMMMMMm::.`./+:://+/./+-+/+//-..........`````````````..-:o+.``.......---:::::/+oyyss/:://+ymMMM
MMMMMMMMMMMMNo-...::-----.://-o/o/+-...............```````..:o:.````......---:/::://oyysy+://+smNMMM
MMMMMMMMMMMMMMd+-..------../:::y++:/.................````../o.````````....--:////++osyhyho//+smNMMMM
MMMMMMMMMMMMMMMMmo:-----..-oy/:/so:/-.....................:o..```````..-:/+ossoosyyhhdhyo///omNMMMMM
MMMMMMMMMMMMMMMMMMmy/:::+ysoss//+ss:/-...................-o:-----::/+osys/--::-/soosyhs/::/+hNMMMMMM
MMMMMMMMMMMMMMMMMMMMNNmNMMdo++o/+oss::-..................-+/++ossssoooossoosyyhyssssyh/::/+hNMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMNmo////sshs::..................-:/-:/ooyyssssossssyssyyhyyhy::/odNMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMmds/::/shds:/-.................::....--/+osyyyyo+oosyyyyyd+:/smNMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMNhhhs/:::ohmh//:--.............-::..`..```..-/++oossooossy+:/hNMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMNdysooso///+shds+/:----------------.......`...-::/+ooossso/+omNMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMNNmdhysyys+/+oo++++sdds+//:----------................--::://///o+mMMMMMMMMMMMMMM
MMNNNNNNNNNNNNNmddhhyssos++sssyo/::+oo++oydhsoo//::------.....``........---::::://o/yNMMMMMMMMMMMMMM
yyyssyyyyyyyyyyyssoooooooo//sy+oso/:::/+oooyhysso++////:---...............-----:/+++mMMMMMMMMMMMMMMM
oooooossssyyyyssoooooooooos/:sh+/+ooo/::-:/+sssssso+++++oo+:--...............--://+hMMMMMMMMMMMMMMMM
++++++ooossssyssoo+oooooooos/:oh+////++++/////+osyyyssoo++shs/:-...........--://+shNMMMMMMMMMMMMMMMM
/////+++ooooossso+++++++++++o+:/sy+++//://///+++++osssossysssyys+::-------:+oooydNMMMMMMMMMMMMMMMMMM
///////+++ooooooo+++/+++///////::/+oso+++/+///::::::::////+ooydhyhhyssoosyhdddddmNMMMMMMMMMMMMMMMMMM
////////++++ooo+++++////////:::::::::/++o+++++++/////////++oydysyhhhhdddddddddddddmmNmNNMMMMMMMMMMMM
::://///++++++++++++///////:::::::::::::::////////////++oshhyssyyyhhhdddddddddddhhhhhhdddmmNNMMMMMMM
/:::///+//++++++///+///////:::::::::::::::::::::::///++ooooosssssyyhhhhhddhhhhhhhhhhyyyyyyhdddmMMMMM
/::://+o////+++/////////:::/:::::::::::::::::::://////++++oooooossyyyhhhhhhhhhhhhyyyssoooossyhdmNMMM
*/

/// *********** Coded by Olium_MdioxideN ***********

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define llu unsigned long long
#define elif else if
#define pb push_back
#define mp make_pair
#define nl '\n'
#define forr(i,b,e) for(int i=b;i<=e;i++)
#define forrv(i,b,e) for(int i=b;i>=e;i--)
#define fors(i,x) for(auto i=x.begin(); i!=x.end(); i++)
#define F first
#define S second
#define mset0(x) memset(x, 0, sizeof x);
#define mset1(x) memset(x, -1, sizeof x);
#define all(x) x.begin(),x.end()
#define pqmin(x) priority_queue<x, vector<x>, greater<x> >
#define pqmax(x) priority_queue<x>
#define sortdsc(x) sort(all(x)); reverse(all(x));
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define mpii map<int,int>
#define pi 3.14159265358979323846D
#define gcd(x,y) __gcd(x,y)
#define lcm(x,y) (x*(y/__gcd(x,y)))
#define chkbit(x,p) ((x&(1LL<<p))?1:0)
#define setbit(x,p) x|=(1LL<<p)
#define clrbit(x,p) x=x&(~(1LL<<p))
#define revbit(x,p) (chkbit(x,p)?clrbit(x,p):setbit(x,p))
#define runtime() cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"
ll pow_mod(ll nn, ll pp, ll mm) { ll res = 1; while(pp > 0) { if(pp&1) res = (res * nn) % mm; pp = pp >> 1; nn = (nn * nn) % mm; } return res; }

#ifndef ONLINE_JUDGE
vector<string>vec_spltr(string s){s+=',';vector<string>res;while(!s.empty())
res.push_back(s.substr(0, s.find(','))),s=s.substr(s.find(',')+1);return res;}
void dbg_out(vector<string>__attribute__((unused))args,__attribute__((unused))int idx,
__attribute__((unused))int LINE_NUM){cerr<<endl;}template<typename Head,typename... Tail>
void dbg_out ( vector <string> args, int idx, int LINE_NUM, Head H, Tail... T)
{if(idx>0)cerr<<", ";else cerr << "Line(" << LINE_NUM << ") ";stringstream ss;ss<<H;
cerr<<args[idx]<<" = "<<ss.str();dbg_out(args,idx+1,LINE_NUM, T...);}
#define debug(...) dbg_out(vec_spltr(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#define in_file freopen("input.c", "r", stdin)
#define out_file freopen("output.c", "w", stdout)
#else
#define debug(...) 0
#define in_file 0
#define out_file 0
#endif

/// * * *    Main Code Starts Now    * * *

#define MOD 10000007
#define MAX 100010
void olium();

int main()
{
    //in_file;
    //out_file;
    fast;
    int t = 1;
    //cin >> t;
    forr(i,1,t)
    {
        //cout << "Case " << i << ": ";
        olium();
    }
    return 0;
}

int n,m;
vector<int> adj[100010];

int dp[100010];

int sol(int cnode)
{
	if(dp[cnode] != -1) return dp[cnode];
	if(adj[cnode].size() == 0) return 0;
	int mx = 0;
	for(auto node : adj[cnode])
	{
		mx = max(mx, 1 + sol(node));
	}
	return dp[cnode] = mx;
}

void olium()
{
	mset1(dp);
    cin >> n >> m;
    forr(i,1,n)
    {
    	adj[i].clear();
    }
    forr(i,1,m)
    {
    	int u,v;
    	cin >> u >> v;
    	adj[u].pb(v);
    }
    int ans = 0;
    forr(i,1,n)
    {
    	ans = max(ans, sol(i));
    }
    cout << ans << nl;
}

Measured memory_bytes: 8192