#include <bits/stdc++.h>
using namespace std;
const int N = 101010;
const int M = 303030;
inline char get(void) {
  static char buf[100000], *S = buf, *T = buf;
  if (S == T) {
    T = (S = buf) + fread(buf, 1, 100000, stdin);
    if (S == T) return EOF;
  }
  return *S++;
}
template <typename T>
inline void read(T &x) {
  static char c;
  x = 0;
  for (c = get(); c < '0' || c > '9'; c = get())
    ;
  for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
}
struct Qry {
  int x, y, l, id;
  inline bool operator<(const Qry &a) const { return l < a.l; }
};
Qry p[M], q[M];
int n, m;
int ans[M];
namespace dsu {
int fa[N], rk[N], sz[N];
int sta[N], drk[N], dsz[N], dcnt[N];
int top, cnt;
inline void Init(int n) {
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= n; i++) sz[i] = 1;
  cnt = n;
}
inline int Fa(int x) {
  while (fa[x] != x) x = fa[x];
  return x;
}
inline int Merge(int x, int y) {
  static int f1, f2;
  f1 = Fa(x);
  f2 = Fa(y);
  if (f1 == f2) return 0;
  ++top;
  dsz[top] = dcnt[top] = drk[top] = 0;
  if (rk[f1] > rk[f2]) swap(f1, f2);
  if (rk[f1] == rk[f2]) {
    ++rk[f2];
    drk[top] = 1;
  }
  fa[f1] = f2;
  sta[top] = f1;
  if (sz[f1] && sz[f2]) {
    cnt -= 2;
    dcnt[top] = -2;
  }
  dsz[top] = sz[f1];
  sz[f2] ^= sz[f1];
  return 1;
}
inline void Restore(int pos) {
  while (top > pos) {
    cnt -= dcnt[top];
    rk[fa[sta[top]]] -= drk[top];
    sz[fa[sta[top]]] ^= dsz[top];
    fa[sta[top]] = sta[top];
    --top;
  }
}
inline bool Check(void) { return !cnt; }
inline void Record(int &lst) { lst = top; }
}  // namespace dsu
inline void DivAndConq(int l, int r, int lo, int hi) {
  int mid = l + r >> 1, mans = -1, lst;
  if (l > r) return;
  dsu::Record(lst);
  for (int i = l; i <= mid; i++)
    if (q[i].id < lo) dsu::Merge(q[i].x, q[i].y);
  for (int i = lo; i <= hi; i++)
    if (p[i].id <= mid) {
      dsu::Merge(p[i].x, p[i].y);
      if (dsu::Check()) {
        mans = i;
        break;
      }
    }
  dsu::Restore(lst);
  if (mans == -1) {
    for (int i = l; i <= mid; i++) ans[i] = -1;
    for (int i = l; i <= mid; i++)
      if (q[i].id < lo) dsu::Merge(q[i].x, q[i].y);
    DivAndConq(mid + 1, r, lo, hi);
    return dsu::Restore(lst);
  }
  ans[mid] = p[mans].l;
  for (int i = l; i <= mid; i++)
    if (q[i].id < lo) dsu::Merge(q[i].x, q[i].y);
  DivAndConq(mid + 1, r, lo, mans);
  dsu::Restore(lst);
  for (int i = lo; i < mans; i++)
    if (p[i].id < l) dsu::Merge(p[i].x, p[i].y);
  DivAndConq(l, mid - 1, mans, hi);
  dsu::Restore(lst);
}
int main(void) {
  read(n);
  read(m);
  for (int i = 1; i <= m; i++) {
    read(p[i].x);
    read(p[i].y);
    read(p[i].l);
    p[i].id = i;
    q[i] = p[i];
  }
  sort(p + 1, p + m + 1);
  for (int i = 1; i <= m; i++) q[p[i].id].id = i;
  dsu::Init(n);
  DivAndConq(1, m, 1, m);
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
  return 0;
}
