1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define IOS ios::sync_with_stdio(0);cout.tie(0);cin.tie(0);
  4. #define endl '\n'
  5. #define double long double
  6. //#define int long long
  7. #define mod 1000000007
  8. #define ll long long
  9.  
  10. int32_t main()
  11. {
  12. #ifndef ONLINE_JUDGE
  13. freopen("int.txt","r",stdin);
  14. freopen("out.txt","w",stdout);
  15. #endif
  16. int n,M;
  17. cin>>n>>M;
  18. vector<vector<pair<int,int>>> g(n+1);
  19. for(int i = 1;i <= M;i++)
  20. {
  21. int u,v,c;
  22. cin>>u>>v>>c;
  23. g[u].push_back({v,c});
  24. g[v].push_back({u,c});
  25. }
  26. deque<int> q;
  27. int s,t;
  28. cin>>s>>t;
  29. vector<int> vis(n+1,0);
  30. vis[s] = 1;
  31. vector<int> cost(n+1,1e7);
  32. cost[s] = 0;
  33. vector<set<int>> clr(n+1);
  34. for(auto it:g[s])
  35. {
  36. cost[it.first] = 0;
  37. q.push_back(it.first);
  38. clr[it.first].insert(it.second);
  39. }
  40. while(!q.empty())
  41. {
  42. int p = q.front();
  43. q.pop_front();
  44. if(p == t)
  45. break;
  46. if(vis[p])
  47. continue;
  48. vis[p] = 1;
  49. for(auto it:g[p])
  50. {
  51. if(!vis[it.first])
  52. {
  53. if(clr[p].find(it.second)!=clr[p].end())
  54. {
  55. if(cost[it.first] > cost[p])
  56. {
  57. clr[it.first].clear();
  58. clr[it.first].insert(it.second);
  59. }
  60. else
  61. if(cost[it.first] == cost[p])
  62. {
  63. clr[it.first].insert(it.second);
  64. }
  65. cost[it.first] = min(cost[it.first],cost[p]);
  66. q.push_front(it.first);
  67. }
  68. else
  69. {
  70. if(cost[it.first] > cost[p] + 1)
  71. {
  72. clr[it.first].clear();
  73. clr[it.first].insert(it.second);
  74. }
  75. else
  76. if(cost[it.first] == cost[p] + 1)
  77. {
  78. clr[it.first].insert(it.second);
  79. }
  80. cost[it.first] = min(cost[it.first],cost[p] + 1);
  81. q.push_back(it.first);
  82. }
  83. }
  84. }
  85. }
  86. cout<<cost[t];
  87. return 0;
  88. }