#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int u;
  int v;
  int c;
  int i;
  bool operator<(const Edge &a) const { return c < a.c; }
  bool operator==(const Edge &a) const {
    return u == a.u && v == a.v && c == a.c;
  }
};
struct Dsu {
  Dsu(const int N) : N(N), p(N) { Reset(); }
  void Reset() {
    for (int i = 0; i < N; ++i) {
      p[i] = i;
    }
  }
  void Join(const int u, const int v) {
    const int pu = FindParent(u);
    const int pv = FindParent(v);
    if (pu != pv) {
      if (rand() & 1) {
        p[pu] = pv;
      } else {
        p[pv] = pu;
      }
    }
  }
  int FindParent(const int u) {
    if (p[u] == u) {
      return u;
    }
    return p[u] = FindParent(p[u]);
  }
  const int N;
  vector<int> p;
};
struct Query {
  int l;
  int r;
  int i;
  int next;
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<Edge> edges(m);
  for (int i = 0; i < m; ++i) {
    cin >> edges[i].u >> edges[i].v >> edges[i].c;
    --edges[i].u;
    --edges[i].v;
    edges[i].i = i;
  }
  sort(edges.begin(), edges.end());
  vector<int> answer(m, -1);
  Dsu dsu(n);
  for (int i = 0; i < m; ++i) {
    const auto e = edges[i];
    if (dsu.FindParent(e.u) == dsu.FindParent(e.v)) {
      answer[e.i] = 1000000000;
      continue;
    }
    dsu.Join(e.u, e.v);
  }
  vector<int> head(m, -1);
  int lastQuery = -1;
  vector<Query> queries(m + 1);
  for (int i = 0; i < m; ++i) {
    const auto e = edges[i];
    if (answer[e.i] == -1) {
      continue;
    }
    Query q;
    q.l = 0;
    q.r = m;
    q.i = i;
    q.next = lastQuery;
    lastQuery = queries.size();
    head[m / 2] = lastQuery;
    queries.push_back(q);
  }
  while (true) {
    bool foundSomething = false;
    dsu.Reset();
    vector<int> rq;
    for (int i = 0; i < m; ++i) {
      const auto e = edges[i];
      if (dsu.FindParent(e.u) != dsu.FindParent(e.v)) {
        dsu.Join(e.u, e.v);
      }
      rq.clear();
      for (int j = head[i]; j != -1; j = queries[j].next) {
        rq.push_back(j);
      }
      head[i] = -1;
      for (size_t j = 0; j < rq.size(); ++j) {
        const int mid = i;
        int lo = queries[rq[j]].l;
        int hi = queries[rq[j]].r;
        const int id = queries[rq[j]].i;
        if (dsu.FindParent(edges[id].u) == dsu.FindParent(edges[id].v)) {
          hi = mid;
        } else {
          lo = mid + 1;
        }
        if (lo == hi) {
          if (lo < m) {
            answer[edges[id].i] = edges[lo].c;
          }
          continue;
        }
        queries[rq[j]].l = lo;
        queries[rq[j]].r = hi;
        queries[rq[j]].next = head[(lo + hi) / 2];
        head[(lo + hi) / 2] = rq[j];
        foundSomething = true;
      }
    }
    if (!foundSomething) {
      break;
    }
  }
  for (int i = 0; i < m; ++i) {
    if (answer[i] != -1) {
      cout << answer[i] << " ";
    }
  }
  return 0;
}
