#include <bits/stdc++.h>
using namespace std;
int n, m, c, l, a, b, p[100010];
vector<pair<int, int> > ans;
set<int> s[100010];
struct edge {
  int u, v, w, i;
} e[1000010];
bool cmp(edge a, edge b) { return a.w < b.w; }
int f(int x) { return x == p[x] ? x : p[x] = f(p[x]); }
int main() {
  cin >> n >> m;
  for (int i = (0); i < (m); i++)
    cin >> e[i].u >> e[i].v >> e[i].w, e[i].i = i, s[e[i].u].insert(i),
                                       s[e[i].v].insert(i);
  for (int i = (1); i < (n + 1); i++) p[i] = i;
  sort(e, e + m, cmp);
  c = 0, l = n - 1;
  while (l) {
    a = f(e[c].u), b = f(e[c].v);
    if (a != b) {
      if (s[a].size() > s[b].size()) swap(a, b);
      for (int i : s[a]) {
        if (s[b].count(i)) {
          if (i != e[c].i) ans.push_back({i, e[c].w});
          s[b].erase(i);
        } else
          s[b].insert(i);
      }
      s[a].clear();
      p[a] = b;
      l--;
    }
    c++;
  }
  sort(ans.begin(), ans.end());
  for (auto p : ans) cout << p.second << '\n';
  return 0;
}
