#include <bits/stdc++.h>
using namespace std;
int root(vector<int>& r, int i) {
  while (r[i] != i) {
    r[i] = r[r[i]];
    i = r[i];
  }
  return i;
}
void wunion(vector<int>& r, vector<int>& size, int a, int b) {
  int root_a = root(r, a);
  int root_b = root(r, b);
  if (root_a != root_b) {
    if (size[root_a] < size[root_b]) {
      r[root_a] = r[root_b];
      size[root_b] += size[root_a];
    } else {
      r[root_b] = r[root_a];
      size[root_a] += size[root_b];
    }
  }
}
vector<vector<int> > jump, jumpc;
vector<int> height;
pair<int, int> ancestor(int i, int j) {
  int k = 0;
  int re = 0;
  while (j) {
    if (j & 1) {
      re = max(re, jumpc[i][k]);
      i = jump[i][k];
    }
    k++;
    j >>= 1;
  }
  return pair<int, int>(i, re);
}
int lca(int i, int j) {
  int hmin = 0, hmax = 1 + min(height[i], height[j]);
  while (hmax - hmin > 1) {
    int hmid = (hmax + hmin) / 2;
    pair<int, int> x = ancestor(i, height[i] - hmid);
    pair<int, int> y = ancestor(j, height[j] - hmid);
    if (x.first == y.first)
      hmin = hmid;
    else
      hmax = hmid;
  }
  return max(ancestor(i, height[i] - hmin).second,
             ancestor(j, height[j] - hmin).second);
}
void initialize(int root, const vector<vector<pair<int, int> > >& t) {
  int n = t.size();
  vector<int> pare(n), cpare(n);
  height = vector<int>(n, -1);
  stack<int> dfs;
  height[root] = 0;
  dfs.push(root);
  pare[root] = root;
  cpare[root] = 0;
  while (!dfs.empty()) {
    int p = dfs.top();
    dfs.pop();
    for (pair<int, int> f : t[p]) {
      if (height[f.second] < 0) {
        height[f.second] = height[p] + 1;
        pare[f.second] = p;
        cpare[f.second] = f.first;
        dfs.push(f.second);
      }
    }
  }
  int m = 1, maux = n;
  while (maux) {
    maux >>= 1;
    m++;
  }
  jumpc = jump = vector<vector<int> >(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    jump[i][0] = pare[i];
    jumpc[i][0] = cpare[i];
  }
  for (int j = 1; j < m; j++) {
    for (int i = 0; i < n; i++) {
      jump[i][j] = jump[jump[i][j - 1]][j - 1];
      jumpc[i][j] = max(jumpc[i][j - 1], jumpc[jump[i][j - 1]][j - 1]);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(20);
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, int> > > g(n);
  vector<pair<pair<int, int>, pair<int, int> > > ed(m);
  for (int i = 0; i < m; i++) {
    int x, y, e;
    cin >> x >> y >> e;
    x--;
    y--;
    ed[i] = {pair<int, int>(e, i), pair<int, int>(x, y)};
    g[y].push_back({e, x});
    g[x].push_back({e, y});
  }
  sort(ed.begin(), ed.end());
  vector<int> r(n), mida(n, 1);
  for (int i = 0; i < n; i++) r[i] = i;
  vector<vector<pair<int, int> > > t(n);
  vector<int> pend;
  for (int i = 0; i < m; i++) {
    int x = ed[i].second.first, y = ed[i].second.second, e = ed[i].first.first;
    if (root(r, x) == root(r, y))
      pend.push_back(i);
    else {
      t[x].push_back({e, y});
      t[y].push_back({e, x});
      wunion(r, mida, x, y);
    }
  }
  initialize(0, t);
  vector<pair<int, int> > sol;
  for (int i : pend) {
    int x = ed[i].second.first, y = ed[i].second.second,
        ind = ed[i].first.second;
    sol.push_back({ind, lca(x, y)});
  }
  sort(sol.begin(), sol.end());
  for (auto x : sol) cout << x.second << "\n";
}
