#include using namespace std; vector topolo(vector>> &Graph){ int siz = Graph.size(); vector ret,inV(siz); for(auto u : Graph) for(auto [to,w] : u) inV.at(to)++; queue Q; for(int i=siz-1; i BFS(vector>> &Graph,int start){ int N = Graph.size(); vector ret(N,-1); queue Q; ret.at(start) = 0,Q.push(start); while(Q.size()){ int pos = Q.front(); Q.pop(); for(auto [to,w] : Graph.at(pos)){ if(ret.at(to) != -1) continue; ret.at(to) = ret.at(pos)+1; Q.push(to); } } return ret; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int N,M; cin >> N >> M; vector>> Graph(N),G(N); for(int i=0; i> a >> b >> c; a--; c--; G.at(c).push_back({a,b}); } vector need = BFS(G,N-1); for(int i=0; i