#include <bits/stdc++.h>
using namespace std;
namespace io {
const int SI = 1 << 21 | 1;
char IB[SI], *IS, *IT, OB[SI], *OS = OB, *OT = OS + SI - 1, c, ch[100];
int f, t;
inline void flush() { fwrite(OB, 1, OS - OB, stdout), OS = OB; }
inline void pc(char x) {
  *OS++ = x;
  if (OS == OT) flush();
}
template <class I>
inline void rd(I &x) {
  for (f = 1, c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
                              IS == IT ? EOF : *IS++)
                            : *IS++);
       c < '0' || c > '9';
       c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
                       IS == IT ? EOF : *IS++)
                     : *IS++))
    if (c == '-') f = -1;
  for (x = 0; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + (c & 15),
      c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
                      IS == IT ? EOF : *IS++)
                    : *IS++))
    ;
  x *= f;
}
inline void rds(char *s, int &x) {
  for (c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
                       IS == IT ? EOF : *IS++)
                     : *IS++);
       c < 33 || c > 126;
       c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
                       IS == IT ? EOF : *IS++)
                     : *IS++))
    ;
  for (x = 0; c >= 33 && c <= 126;
       s[++x] = c, c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
                                   IS == IT ? EOF : *IS++)
                                 : *IS++))
    ;
  s[x + 1] = '\0';
}
template <class I>
inline void print(I x, char k = '\n') {
  if (!x) pc('0');
  if (x < 0) pc('-'), x = -x;
  while (x) ch[++t] = x % 10 + '0', x /= 10;
  while (t) pc(ch[t--]);
  pc(k);
}
inline void prints(string s) {
  int x = s.length();
  while (t < x) pc(s[t++]);
  pc('\n'), t = 0;
}
struct Flush {
  ~Flush() { flush(); }
} flusher;
}  // namespace io
using io::print;
using io::prints;
using io::rd;
using io::rds;
const int N = 4e5 + 7, M = 3e5 + 7;
int n, m, x[M], y[M], z[M], v[M], cnt;
priority_queue<pair<int, int>> q;
struct LCT {
  int f[N], ch[N][2], s[N], v[N], a[N], c[N], d[N];
  inline void upd(int p) {
    s[p] = s[ch[p][0]] + s[ch[p][1]] + d[p] + (p <= n);
    int o = a[c[ch[p][0]]] > a[c[ch[p][1]]] ? c[ch[p][0]] : c[ch[p][1]];
    c[p] = a[o] > a[p] ? o : p;
  }
  inline void spd(int p) {
    if (p && v[p]) {
      if (ch[p][0]) v[ch[p][0]] ^= 1, swap(ch[ch[p][0]][0], ch[ch[p][0]][1]);
      if (ch[p][1]) v[ch[p][1]] ^= 1, swap(ch[ch[p][1]][0], ch[ch[p][1]][1]);
      v[p] = 0;
    }
  }
  inline void rot(int p) {
    int x = f[p], y = f[x], u = (p == ch[f[p]][1]), v = (x == ch[f[x]][1]),
        o = (ch[f[x]][0] != x && ch[f[x]][1] != x);
    f[ch[p][u ^ 1]] = x, ch[x][u] = ch[p][u ^ 1];
    f[x] = p, ch[p][u ^ 1] = x, upd(x), upd(p);
    if ((f[p] = y) && !o) ch[y][v] = p;
  }
  void pre(int p) {
    if (!(ch[f[p]][0] != p && ch[f[p]][1] != p)) pre(f[p]);
    spd(p);
  }
  inline void splay(int p) {
    pre(p);
    for (int x = f[p]; x = f[p], !(ch[f[p]][0] != p && ch[f[p]][1] != p);
         rot(p))
      if (!(ch[f[x]][0] != x && ch[f[x]][1] != x))
        rot((p == ch[f[p]][1]) == (x == ch[f[x]][1]) ? x : p);
  }
  inline void access(int p) {
    for (int x = 0; p; p = f[x = p])
      splay(p), d[p] += s[ch[p][1]], d[p] -= s[ch[p][1] = x], upd(p);
  }
  inline void mkrt(int p) {
    access(p), splay(p), v[p] ^= 1, swap(ch[p][0], ch[p][1]);
  }
  inline void split(int x, int y) { mkrt(x), access(y), splay(y); }
  inline int fdrt(int p) {
    access(p), splay(p);
    while (ch[p][0]) spd(p), p = ch[p][0];
    return splay(p), p;
  }
  inline void link(int x, int y) {
    mkrt(x), access(y), splay(y), cnt -= s[x] & 1, cnt -= s[y] & 1;
    d[f[x] = y] += s[x], upd(y), cnt += s[y] & 1;
  }
  inline void cut(int x, int y) {
    split(x, y), cnt -= s[y] & 1;
    f[x] = ch[y][0] = 0, upd(y), cnt += s[x] & 1, cnt += s[y] & 1;
  }
  inline int add(int i) {
    int x = ::x[i], y = ::y[i], z = ::z[i];
    bool ok = 1;
    if (fdrt(x) == fdrt(y)) {
      split(x, y);
      int o = c[y];
      if (a[o] > z)
        cut(::x[o - n], o), cut(::y[o - n], o), v[o] = 1;
      else
        ok = 0;
    }
    if (ok) {
      s[i + n] = 1, a[i + n] = z, c[i + n] = i + n;
      link(x, i + n), link(y, i + n), q.push(make_pair(z, i));
    }
    if (cnt) return -1;
    while (q.size()) {
      int o = q.top().second;
      q.pop();
      if (::v[o]) continue;
      cut(::x[o], o + n), cut(::y[o], o + n);
      if (cnt) {
        link(::x[o], o + n), link(::y[o], o + n);
        return q.push(make_pair(::z[o], o)), ::z[o];
      }
    }
    return 0;
  }
} lct;
int main() {
  rd(n), rd(m), cnt = n;
  for (int i = 1; i <= n; i++) lct.s[i] = 1, lct.c[i] = i;
  for (int i = 1; i <= m; i++) rd(x[i]), rd(y[i]), rd(z[i]), print(lct.add(i));
  return 0;
}
