#include <bits/stdc++.h>
using namespace std;
int read() {
  int X = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') X = X * 10 + c - '0', c = getchar();
  return X * w;
}
const int N = 400000 + 10;
int n, m, X[N], Y[N], Z[N];
priority_queue<pair<int, int> > Q;
int fa[N], ch[N][2], w[N], mx[N], sz[N], lsz[N], del[N], rev[N], sta[N], cnt;
int nroot(int x) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; }
void pushup(int x) {
  sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + lsz[x] + (x <= n);
  int y = w[mx[ch[x][0]]] > w[mx[ch[x][1]]] ? mx[ch[x][0]] : mx[ch[x][1]];
  mx[x] = w[x] > w[y] ? x : y;
}
void pushdown(int x) {
  if (rev[x]) {
    if (ch[x][0]) rev[ch[x][0]] ^= 1, swap(ch[ch[x][0]][0], ch[ch[x][0]][1]);
    if (ch[x][1]) rev[ch[x][1]] ^= 1, swap(ch[ch[x][1]][0], ch[ch[x][1]][1]);
    rev[x] = 0;
  }
}
void rotate(int x) {
  int y = fa[x], z = fa[y], k = (x == ch[y][1]), w = ch[x][!k];
  if (nroot(y)) ch[z][y == ch[z][1]] = x;
  ch[x][!k] = y, ch[y][k] = w;
  if (w) fa[w] = y;
  fa[y] = x, fa[x] = z;
  pushup(y);
}
void splay(int x) {
  int y = x, z = 0;
  sta[++z] = y;
  while (nroot(y)) sta[++z] = y = fa[y];
  while (z) pushdown(sta[z--]);
  while (nroot(x)) {
    y = fa[x], z = fa[y];
    if (nroot(y)) rotate((x == ch[y][0] ^ (y == ch[z][0])) ? x : y);
    rotate(x);
  }
  pushup(x);
}
void access(int x) {
  for (int y = 0; x; x = fa[y = x])
    splay(x), lsz[x] += sz[ch[x][1]], lsz[x] -= sz[ch[x][1] = y], pushup(x);
}
void makeroot(int x) {
  access(x), splay(x), rev[x] ^= 1, swap(ch[x][0], ch[x][1]);
}
int findroot(int x) {
  access(x), splay(x);
  while (ch[x][0]) pushdown(x), x = ch[x][0];
  return x;
}
void split(int x, int y) { makeroot(x), access(y), splay(y); }
void link(int x, int y) {
  makeroot(x), access(y), splay(y);
  cnt -= sz[x] & 1, cnt -= sz[y] & 1;
  lsz[fa[x] = y] += sz[x], pushup(y);
  cnt += sz[y] & 1;
}
void cut(int x, int y) {
  split(x, y);
  cnt -= sz[y] & 1;
  fa[x] = ch[y][0] = 0, pushup(y);
  cnt += sz[x] & 1, cnt += sz[y] & 1;
}
int solve(int i) {
  int x = X[i], y = Y[i], z = Z[i];
  if (findroot(x) == findroot(y)) {
    split(x, y);
    int p = mx[y];
    if (w[p] > z) {
      del[p - n] = 1;
      sz[i + n] = 1, w[i + n] = z, mx[i + n] = i + n;
      cut(X[p - n], p), cut(Y[p - n], p);
      link(x, i + n), link(y, i + n);
      Q.push(make_pair(z, i));
    }
  } else {
    sz[i + n] = 1, w[i + n] = z, mx[i + n] = i + n;
    link(x, i + n), link(y, i + n);
    Q.push(make_pair(z, i));
  }
  if (cnt) return -1;
  while (!Q.empty()) {
    int p = Q.top().second;
    Q.pop();
    if (del[p]) continue;
    cut(X[p], p + n), cut(Y[p], p + n);
    if (cnt) {
      Q.push(make_pair(Z[p], p));
      link(X[p], p + n), link(Y[p], p + n);
      return Z[p];
    }
  }
  return 0;
}
int main() {
  cnt = n = read(), m = read();
  for (int i = 1; i <= n; ++i) sz[i] = 1, mx[i] = i;
  for (int i = 1; i <= m; ++i) {
    X[i] = read(), Y[i] = read(), Z[i] = read();
    printf("%d\n", solve(i));
  }
  return 0;
}
