#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
struct Edge {
  int u, v, w;
  Edge() {}
  Edge(int _u, int _v, int _w) : u(_u), v(_v), w(_w) {}
};
bool cmp(Edge a, Edge b) { return (a.w < b.w); }
int n, m;
vector<Edge> e, originE;
vector<pair<int, int> > adj[MAXN];
pair<int, int> st[MAXN][21];
int d[MAXN], p[MAXN];
class DSU {
  int sz;
  vector<int> p;

 public:
  DSU() {}
  DSU(int _sz) : sz(_sz) {
    p.resize(sz + 1);
    for (int i = 0; i <= sz; ++i) {
      p[i] = i;
    }
  }
  int getParent(int x) {
    if (p[x] == x)
      return x;
    else
      return p[x] = getParent(p[x]);
  }
  void conn(int a, int b) {
    int pa = getParent(a), pb = getParent(b);
    if (pa != pb) {
      p[pa] = pb;
    }
  }
} dsu;
void buildTree(int pos, int depth) {
  d[pos] = depth;
  for (pair<int, int> i : adj[pos])
    if (p[pos] != i.first) {
      p[i.first] = pos;
      st[i.first][0] = pair<int, int>(p[i.first], i.second);
      buildTree(i.first, depth + 1);
    }
}
void buildST() {
  for (int j = 1; j < 20; ++j) {
    for (int i = 1; i <= n; ++i) {
      if (st[i][j - 1].first == -1) {
        st[i][j].first = -1;
        st[i][j].second = st[i][j - 1].second;
      } else {
        st[i][j].first = st[st[i][j - 1].first][j - 1].first;
        st[i][j].second =
            max(st[i][j - 1].second, st[st[i][j - 1].first][j - 1].second);
      }
    }
  }
}
pair<int, int> getLCA(int a, int b) {
  int maxW = 0;
  if (d[a] < d[b]) swap(a, b);
  for (int i = 19; i >= 0; --i) {
    if (d[a] - (1 << i) >= d[b]) {
      maxW = max(maxW, st[a][i].second);
      a = st[a][i].first;
    }
  }
  if (a == b) return pair<int, int>(a, maxW);
  for (int i = 19; i >= 0; --i) {
    if (st[a][i].first != st[b][i].first) {
      maxW = max(maxW, st[a][i].second);
      maxW = max(maxW, st[b][i].second);
      a = st[a][i].first;
      b = st[b][i].first;
    }
  }
  maxW = max(maxW, max(st[a][0].second, st[b][0].second));
  return pair<int, int>(a, maxW);
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < MAXN; ++i) {
    for (int j = 0; j < 20; ++j) {
      st[i][j] = pair<int, int>(-1, 0);
    }
  }
  for (int i = 0; i < m; ++i) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w);
    e.push_back(Edge(u, v, w));
    originE.push_back(Edge(u, v, w));
  }
  sort(e.begin(), e.end(), cmp);
  dsu = DSU(n);
  for (Edge i : e) {
    if (dsu.getParent(i.u) != dsu.getParent(i.v)) {
      dsu.conn(i.u, i.v);
      adj[i.u].push_back(pair<int, int>(i.v, i.w));
      adj[i.v].push_back(pair<int, int>(i.u, i.w));
    }
  }
  buildTree(1, 0);
  buildST();
  for (Edge i : originE) {
    if (p[i.u] == i.v || p[i.v] == i.u) continue;
    printf("%d\n", getLCA(i.u, i.v).second);
  }
}
