#include <bits/stdc++.h>
using namespace std;
vector<pair<int, pair<int, int> > > V;
vector<pair<int, int> > adj[100007];
int P[100007];
vector<pair<int, int> > T;
int parent(int v) { return P[v] = ((P[v] == v) ? (v) : parent(P[v])); }
bool same(int u, int v) { return parent(u) == parent(v); }
void join(int v, int u) {
  if (!same(u, v)) {
    P[parent(v)] = parent(u);
  }
}
int n, m;
const int MX = 100009, MXL = 18;
vector<int> v[MX];
int QN, depth[MX], par[MXL][MX];
int maxi[MXL][MX];
int sum[MX];
void Pdfs(int first, int p) {
  int nxt, C, sz = adj[first].size();
  for (int j = 0; j < sz; j++) {
    nxt = adj[first][j].first;
    if (nxt == p) continue;
    depth[nxt] = depth[first] + 1;
    par[0][nxt] = first;
    maxi[0][nxt] = adj[first][j].second;
    Pdfs(nxt, first);
  }
}
void process() {
  for (int j = 1; j < MXL; j++)
    for (int i = 1; i <= n; i++) {
      par[j][i] = par[j - 1][par[j - 1][i]];
      maxi[j][i] = max(maxi[j - 1][i], maxi[j - 1][par[j - 1][i]]);
    }
}
pair<int, int> Kth(int first, int K) {
  int node = first;
  int M = 0;
  for (int j = 0; j < MXL; j++)
    if ((K & (1 << j))) {
      M = max(M, maxi[j][node]);
      node = par[j][node];
    }
  return make_pair(node, M);
}
int LCA(int a, int b) {
  int M = 0;
  if (depth[a] > depth[b]) swap(a, b);
  pair<int, int> P = Kth(b, depth[b] - depth[a]);
  b = P.first;
  M = P.second;
  if (a == b) return M;
  for (int j = MXL - 1; j >= 0; j--)
    if (par[j][a] != par[j][b]) {
      M = max(M, max(maxi[j][a], maxi[j][b]));
      a = par[j][a], b = par[j][b];
    }
  M = max(M, max(maxi[0][a], maxi[0][b]));
  return M;
}
vector<pair<int, pair<int, int> > > X;
map<int, int> M;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) P[i] = i;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    V.push_back(make_pair(c, make_pair(a, b)));
    M[c] = i;
  }
  sort(V.begin(), V.end());
  for (int i = 0; i < V.size(); i++) {
    int a = V[i].second.first;
    int b = V[i].second.second;
    int c = V[i].first;
    if (!same(a, b)) {
      join(a, b);
      adj[a].push_back(make_pair(b, c));
      adj[b].push_back(make_pair(a, c));
    } else {
      X.push_back(V[i]);
    }
  }
  depth[1] = 1;
  Pdfs(1, -1);
  process();
  for (int i = 0; i < X.size(); i++) {
    T.push_back(
        make_pair(M[X[i].first], LCA(X[i].second.first, X[i].second.second)));
  }
  sort(T.begin(), T.end());
  for (int i = 0; i < T.size(); i++) cout << T[i].second << " ";
  cout << endl;
  return 0;
}
