Language: C++

#include <bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef pair<int, int> ii;
typedef vector<int> vi;
#define mkpr make_pair

const int N = 1e5 + 5;
const int M = 2e5 + 5;

int n, m;

vector<vector<ii>> edgeList(M);
vector<bool> vis(N, false);
vector<bool> inStack(N, false);
vector<bool> groupVis(M, false);

vector<vector<int>> vsGroups(N); 
vector<vector<int>> groupsVs(M);

map<int, int> colorReindexer; //colorReindexer[oldInd] gives newInd
int minUnusedIndex;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m;

	minUnusedIndex = 0;
	int p, q, c;
	for(int i=0; i<m; i++) {
		cin >> p >> q >> c;
		p--; q--; c--;
		if(colorReindexer.find(c) == colorReindexer.end()) {
			colorReindexer.insert(mkpr(c, minUnusedIndex++));
		}
		int cIndex = colorReindexer[c];
		edgeList[cIndex].push_back(mkpr(p, q));
		edgeList[cIndex].push_back(mkpr(q, p));
	}

	//Process by color
	int nGroup = 0;
	for(int i = 0; i < minUnusedIndex; i++) {
		vector<ii> companyProperty;
		companyProperty.reserve(edgeList[i].size());
		
		for(ii e: edgeList[i]) {
			companyProperty.push_back(e);
			vis[e.first] = inStack[e.first] = false;
			vis[e.second] = inStack[e.second] = false;
		}

		sort(companyProperty.begin(), companyProperty.end());
		int unusedEdge = 0;
		stack<int> st;
		st.push(companyProperty[unusedEdge].first);
		st.push(companyProperty[unusedEdge].second);
		inStack[companyProperty[unusedEdge].first] = true;
		inStack[companyProperty[unusedEdge].second] = true;
		while(unusedEdge < companyProperty.size()) {
			while(!st.empty()) {
				int u = st.top(); st.pop();
				if(vis[u]) continue;
				vis[u] = true;

				vsGroups[u].push_back(nGroup);
				groupsVs[nGroup].push_back(u);

				int starter = lower_bound(companyProperty.begin(), companyProperty.end(), mkpr(u, 0)) - companyProperty.begin();
				int ender = upper_bound(starter, companyProperty.end(), mkpr(u, N)) - companyProperty.begin();
				for(int i = starter; i != ender; i++) {
					ii v = companyProperty[i];
					if(inStack[v.second]) continue;
					st.push(v.second);
					inStack[v.second] = true;
				}
			}
			nGroup++;

			while(unusedEdge < companyProperty.size() && vis[companyProperty[unusedEdge].first]) unusedEdge++;
		}
	}

	for(int i = 0; i < n; i++) vis[i] = false;
	for(int i = 0; i < m; i++) groupVis[i] = false;

	int ans = 0;
	stack<int> st;
	stack<int> newSt;
	st.push(0);
	
	bool nReached = false;

	while(!st.empty() && !nReached) {
		stack<int> groupStack;
		while(!st.empty()) {
			int u = st.top(); st.pop();
			for(int g: vsGroups[u]) {
				if(!groupVis[g]) {
					groupStack.push(g);
					groupVis[g] = true;
				}
			}
		}

		while(!groupStack.empty()) {
			int g = groupStack.top(); groupStack.pop();
			for(int u: groupsVs[g]) {
				if(!vis[u]) {
					if(u == n-1) nReached = true;
					newSt.push(u);
					vis[u] = true;
				}
			}
		}

		ans++;
		while(!newSt.empty()) {
			st.push(newSt.top()); newSt.pop();
		}
	}

	cout << (nReached? ans:-1) << endl;

	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:72:56: error: no matching function for call to 'upper_bound(int&, std::vector<std::pair<int, int> >::iterator, std::pair<int, int>)'
   72 |                                 int ender = upper_bound(starter, companyProperty.end(), mkpr(u, N)) - companyProperty.begin();
      |                                             ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:61,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algo.h:2019:5: note: candidate: 'template<class _FIter, class _Tp> _FIter std::upper_bound(_FIter, _FIter, const _Tp&)'
 2019 |     upper_bound(_ForwardIterator __first, _ForwardIterator __last,
      |     ^~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:2019:5: note:   template argument deduction/substitution failed:
a.cc:72:56: note:   deduced conflicting types for parameter '_FIter' ('int' and '__gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int> > >')
   72 |                                 int ender = upper_bound(starter, companyProperty.end(), mkpr(u, N)) - companyProperty.begin();
      |                                             ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:2050:5: note: candidate: 'template<class _FIter, class _Tp, class _Compare> _FIter std::upper_bound(_FIter, _FIter, const _Tp&, _Compare)'
 2050 |     upper_bound(_ForwardIterator __first, _ForwardIterator __last,
      |     ^~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:2050:5: note:   candidate expects 4 arguments, 3 provided