#include <bits/stdc++.h>
using namespace std;
const int inft = 1000000009;
const int MAXN = 1000006;
pair<int, int> P[MAXN];
int L[MAXN];
int n, m;
vector<pair<int, int> > X;
struct DSU {
  int n, odd;
  int size[MAXN], f[MAXN];
  vector<pair<int *, int> > changes;
  void init(int _n) {
    n = _n;
    for (int i = 0; i < (n); ++i) f[i] = i;
    for (int i = 0; i < (n); ++i) size[i] = 1;
    odd = n;
  }
  int _find(int first) {
    while (first != f[first]) first = f[first];
    return first;
  }
  void set(int *a, int b) {
    changes.push_back(make_pair(a, *a));
    *a = b;
  }
  void _union(int a, int b) {
    a = _find(a);
    b = _find(b);
    ;
    if (a == b) return;
    if (size[a] < size[b]) swap(a, b);
    set(f + b, a);
    set(&odd, odd + (-size[a] % 2 - size[b] % 2 + (size[a] + size[b]) % 2));
    set(size + a, size[a] + size[b]);
  }
  void unio(int w) {
    ;
    _union(P[w].first, P[w].second);
  }
  void rollback(int tim) {
    while (changes.size() > tim) {
      *changes.back().first = changes.back().second;
      changes.pop_back();
    };
    for (int i = 0; i < (n); ++i)
      ;
    ;
  }
  int backup() { return changes.size(); }
} T;
vector<int> ANS;
void go(int l, int r, int lo, int hi) {
  ;
  ;
  for (int i = 0; i < (n); ++i)
    ;
  ;
  if (l > r) return;
  int mi = (l + r) / 2;
  int p0 = T.backup();
  ;
  for (int i = l; i <= mi; i++)
    if (L[i] < lo) T.unio(i);
  int j = lo;
  ;
  while (T.odd != 0 && j <= hi) {
    if (X[j].second <= mi) T.unio(X[j].second);
    j++;
  }
  j--;
  int sklad = T.odd;
  ;
  T.rollback(p0);
  if (sklad) {
    for (int i = l; i <= mi; i++) ANS[i] = -1;
    ;
    for (int i = l; i <= mi; i++)
      if (L[i] < lo) T.unio(i);
    go(mi + 1, r, lo, hi);
    T.rollback(p0);
    return;
  }
  ANS[mi] = j;
  for (int i = lo; i < j; i++)
    if (X[i].second < l) T.unio(X[i].second);
  go(l, mi - 1, j, hi);
  T.rollback(p0);
  for (int i = l; i <= mi; i++)
    if (L[i] < lo) T.unio(i);
  go(mi + 1, r, lo, j);
  T.rollback(p0);
}
void solve() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < (m); ++i) {
    scanf("%d%d%d", &P[i].first, &P[i].second, &L[i]);
    X.push_back(pair<int, int>(L[i], i));
  }
  for (int i = 0; i < (m); ++i) {
    P[i].first--;
    P[i].second--;
  }
  ANS.resize(m);
  sort((X).begin(), (X).end());
  for (int i = 0; i < (m); ++i)
    L[i] = lower_bound((X).begin(), (X).end(), pair<int, int>(L[i], i)) -
           X.begin();
  T.init(n);
  go(0, m - 1, 0, m - 1);
  for (int i = 0; i < (m); ++i)
    if (ANS[i] != -1) ANS[i] = X[ANS[i]].first;
  for (int i = 0; i < (m); ++i) printf("%d ", ANS[i]);
}
int main() {
  int t = 1;
  for (int i = 0; i < (t); ++i) solve();
  return 0;
}
