#include <bits/stdc++.h>
using namespace std;
const int max_n = 100111, max_e = 1000011, inf = 1000111222;
int pr[max_n];
int sz[max_n];
int n, m;
vector<pair<pair<int, int>, pair<int, int>>> f;
vector<pair<pair<int, int>, pair<int, int>>> e;
vector<pair<pair<int, int>, pair<int, int>>> alle;
int get_set(int cur) { return pr[cur] == cur ? cur : cur = get_set(pr[cur]); }
void unite(int a, int b) {
  int pa = get_set(a);
  int pb = get_set(b);
  if (sz[pa] > sz[pb]) {
    pr[pb] = pa;
    sz[pa] += sz[pb];
  } else {
    pr[pa] = pb;
    sz[pb] += sz[pa];
  }
}
void clr() {
  for (int i = 0; i < n; ++i) {
    pr[i] = i;
    sz[i] = 1;
  }
}
int l[max_e], r[max_e], mid[max_e];
int ans[max_e];
bool res[max_e];
void split() {
  clr();
  for (auto c : alle) {
    int a = c.second.first, b = c.second.second;
    bool added;
    if (get_set(a) != get_set(b)) {
      added = true;
      unite(a, b);
    } else {
      added = false;
    }
    if (added) {
      e.push_back(c);
    } else {
      f.push_back(c);
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    pair<pair<int, int>, pair<int, int>> c;
    scanf("%d%d%d", &c.second.first, &c.second.second, &c.first.first);
    --c.second.second;
    --c.second.first;
    c.first.second = i;
    alle.push_back(c);
  }
  sort(alle.begin(), alle.end());
  split();
  for (int i = 0; i < m; ++i) {
    l[i] = 0;
    r[i] = 1000000001;
    ans[i] = -1;
  }
  int fnd = 0;
  while (fnd < f.size()) {
    vector<pair<pair<int, int>, pair<int, int>>> q;
    for (pair<pair<int, int>, pair<int, int>> ed : f) {
      int i = ed.first.second;
      if (ans[i] != -1) continue;
      mid[i] = (1LL * l[i] + r[i]) / 2;
      ed.first.first = mid[i];
      q.push_back(ed);
    }
    sort(q.begin(), q.end());
    reverse(q.begin(), q.end());
    clr();
    for (auto c : e) {
      while (q.size() && q.back().first.first <= c.first.first) {
        int a = q.back().second.first, b = q.back().second.second,
            i = q.back().first.second;
        q.pop_back();
        if (get_set(a) == get_set(b)) {
          res[i] = 0;
        } else {
          res[i] = 1;
        }
      }
      int a = c.second.first, b = c.second.second;
      if (get_set(a) != get_set(b)) {
        unite(a, b);
      }
    }
    while (q.size()) {
      int i = q.back().first.second;
      q.pop_back();
      res[i] = 0;
    }
    for (pair<pair<int, int>, pair<int, int>> ed : f) {
      int i = ed.first.second;
      if (ans[i] != -1) continue;
      if (res[i] == 1) {
        l[i] = mid[i];
      } else {
        r[i] = mid[i];
      }
      if (l[i] + 1 == r[i]) {
        ans[i] = l[i];
        ++fnd;
      }
    }
  }
  for (int i = 0; i < m; ++i) {
    if (ans[i] != -1) {
      printf("%d\n", ans[i]);
    }
  }
  return 0;
}
