#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const long long inff = 0x3f3f3f3f3f3f3f3f;
const int N = 200008;
int deep[N], fa[N][20], n, m, st[N][20], f[N], ans[1000008];
vector<pair<int, int> > g[N];
struct as {
  int x, y, z, id;
} a[1000008];
void dfs(int u, int f) {
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i].first;
    if (v == f) continue;
    deep[v] = deep[u] + 1, dfs(v, u), fa[v][0] = u, st[v][0] = g[u][i].second;
  }
}
void bz() {
  for (int i(1); i <= (18); ++i)
    for (int j(1); j <= (n); ++j)
      fa[j][i] = fa[fa[j][i - 1]][i - 1],
      st[j][i] = max(st[j][i - 1], st[fa[j][i - 1]][i - 1]);
}
int lca(int x, int y) {
  if (deep[x] < deep[y]) swap(x, y);
  int d = deep[x] - deep[y], ans = 0;
  for (int i(0); i <= (18); ++i)
    if (d & (1 << i)) ans = max(st[x][i], ans), x = fa[x][i];
  if (x == y) return ans;
  for (int i(18); i >= (0); --i)
    if (fa[x][i] != fa[y][i])
      ans = max(st[x][i], ans), ans = max(st[y][i], ans), x = fa[x][i],
      y = fa[y][i];
  return max(ans, max(st[x][0], st[y][0]));
}
bool cmp(as a, as b) { return a.z < b.z; }
int fid(int x) {
  int a = x, b;
  while (a != f[a]) a = f[a];
  while (x != f[x]) b = f[x], f[x] = a, x = b;
  return a;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i(1); i <= (n); ++i) f[i] = i;
  for (int i(1); i <= (m); ++i)
    scanf("%d", &a[i].x), scanf("%d", &a[i].y), scanf("%d", &a[i].z),
        a[i].id = i;
  sort(a + 1, a + m + 1, cmp);
  for (int i(1); i <= (m); ++i) {
    int fx = fid(a[i].x), fy = fid(a[i].y);
    if (fx == fy) {
      ans[a[i].id] = i;
      continue;
    }
    f[fx] = fy;
    g[a[i].x].push_back(pair<int, int>(a[i].y, a[i].z));
    g[a[i].y].push_back(pair<int, int>(a[i].x, a[i].z));
  }
  dfs(1, 0), bz();
  for (int i(1); i <= (m); ++i)
    if (ans[i]) printf("%d\n", lca(a[ans[i]].x, a[ans[i]].y));
  return 0;
}
