#include <bits/stdc++.h>
using namespace std;
int getint() {
  char c;
  for (c = getchar(); c < '0' || c > '9'; c = getchar())
    ;
  int t = c - 48;
  for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) t = t * 10 + c - 48;
  return t;
}
void putint(long long n) {
  char a[20];
  int size = 0;
  if (n == 0) putchar('0');
  while (n) {
    a[size++] = n % 10 + 48;
    n /= 10;
  }
  for (int i = size - 1; i >= 0; i--) putchar(a[i]);
  putchar('\n');
}
int n, m, x, y, z, fa[300000], anc[300000][20], depth[300000];
vector<pair<int, int> > a[300000];
pair<pair<int, int>, pair<int, int> > e[2000000];
bool b[2000000];
long long mst, ans[2000000], cost[300000][20];
int getf(int x) {
  if (x == fa[x])
    return x;
  else {
    fa[x] = getf(fa[x]);
    return fa[x];
  }
}
void dfs(int x, int l) {
  for (int i = 0; i < a[x].size(); i++)
    if (l != a[x][i].first) {
      anc[a[x][i].first][0] = x;
      cost[a[x][i].first][0] = a[x][i].second;
      depth[a[x][i].first] = depth[x] + 1;
      dfs(a[x][i].first, x);
    }
}
void init() {
  for (int k = 1; k < 20; k++)
    for (int i = 1; i <= n; i++) {
      if (anc[i][k - 1] == 0) continue;
      anc[i][k] = anc[anc[i][k - 1]][k - 1];
      cost[i][k] = max(cost[i][k - 1], cost[anc[i][k - 1]][k - 1]);
    }
}
long long get(int u, int v) {
  if (depth[u] < depth[v]) swap(u, v);
  long long t = 0;
  for (int k = 19; k >= 0; k--) {
    if (anc[u][k] == 0) continue;
    if (depth[anc[u][k]] >= depth[v]) {
      t = max(t, cost[u][k]);
      u = anc[u][k];
      if (depth[u] == depth[v]) break;
    }
  }
  if (u == v) return t;
  for (int k = 19; k >= 0; k--) {
    if (anc[u][k] == 0) continue;
    if (anc[u][k] != anc[v][k]) {
      t = max(t, max(cost[u][k], cost[v][k]));
      u = anc[u][k];
      v = anc[v][k];
    }
  }
  return max(t, max(cost[u][0], cost[v][0]));
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    x = getint();
    y = getint();
    z = getint();
    e[i] = make_pair(make_pair(z, i), make_pair(x, y));
  }
  sort(e + 1, e + m + 1);
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= m; i++) {
    x = getf(e[i].second.first);
    y = getf(e[i].second.second);
    if (x == y) continue;
    mst += e[i].first.first;
    b[i] = true;
    a[e[i].second.first].push_back(
        make_pair(e[i].second.second, e[i].first.first));
    a[e[i].second.second].push_back(
        make_pair(e[i].second.first, e[i].first.first));
    fa[x] = y;
  }
  depth[1] = 1;
  dfs(1, 0);
  init();
  for (int i = 1; i <= m; i++) {
    ans[e[i].first.second] = mst;
    if (b[i]) {
      ans[e[i].first.second] = -1;
      continue;
    }
    ans[e[i].first.second] = get(e[i].second.first, e[i].second.second);
  }
  for (int i = 1; i <= m; i++)
    if (ans[i] != -1) printf("%lld\n", ans[i]);
  return 0;
}
