#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 5;
int n, m, cnt, u[N], v[N], w[N], fa[N], ch[2][N], rev[N], sz[N], vir[N], mx[N],
    pos[N];
bool son(int x) { return x == ch[1][fa[x]]; }
bool isroot(int x) { return x != ch[0][fa[x]] && x != ch[1][fa[x]]; }
void up(int x) {
  mx[x] = w[x];
  pos[x] = x;
  if (mx[ch[0][x]] > mx[x]) mx[x] = mx[ch[0][x]], pos[x] = pos[ch[0][x]];
  if (mx[ch[1][x]] > mx[x]) mx[x] = mx[ch[1][x]], pos[x] = pos[ch[1][x]];
  sz[x] = (x <= n) + sz[ch[0][x]] + sz[ch[1][x]] + vir[x];
}
void rotate(int x) {
  int y = fa[x], z = fa[y], c = son(x);
  ch[c][y] = ch[c ^ 1][x];
  if (ch[c][y]) fa[ch[c][y]] = y;
  fa[x] = z;
  if (!isroot(y)) ch[son(y)][z] = x;
  ch[c ^ 1][x] = y;
  fa[y] = x;
  up(y);
}
void rever(int x) {
  if (x) swap(ch[0][x], ch[1][x]), rev[x] ^= 1;
}
void down(int x) {
  if (rev[x]) rever(ch[0][x]), rever(ch[1][x]), rev[x] = 0;
}
void alldown(int x) {
  if (!isroot(x)) alldown(fa[x]);
  down(x);
}
void splay(int x) {
  alldown(x);
  for (int y = fa[x]; !isroot(x); rotate(x), y = fa[x])
    if (!isroot(y)) rotate(son(x) ^ son(y) ? x : y);
  up(x);
}
void access(int x) {
  for (int y = 0; x; y = x, x = fa[x]) {
    splay(x);
    vir[x] += sz[ch[1][x]];
    ch[1][x] = y;
    vir[x] -= sz[y];
    up(x);
  }
}
void makeroot(int x) {
  access(x);
  splay(x);
  rever(x);
}
int findroot(int x) {
  access(x);
  splay(x);
  while (ch[0][x]) down(x), x = ch[0][x];
  splay(x);
  return x;
}
void split(int x, int y) {
  makeroot(x);
  access(y);
  splay(y);
}
void link(int x, int y) {
  makeroot(x);
  makeroot(y);
  fa[x] = y;
  vir[y] += sz[x];
  up(y);
}
void cut(int x, int y) {
  split(x, y);
  ch[0][y] = fa[x] = 0;
  up(y);
}
int getsize(int x) {
  makeroot(x);
  return sz[x];
}
struct cmp {
  bool operator()(int x, int y) { return w[x] < w[y] || w[x] == w[y] && x < y; }
};
set<int, cmp> S;
int main() {
  scanf("%d%d", &n, &m);
  cnt = n;
  for (int i = 1; i <= n; ++i) sz[i] = 1;
  for (int i = n + 1; i <= n + m; ++i) {
    scanf("%d%d%d", &u[i], &v[i], &w[i]);
    up(i);
    if (findroot(u[i]) != findroot(v[i])) {
      if (getsize(u[i]) & getsize(v[i]) & 1) cnt -= 2;
      link(u[i], i);
      link(v[i], i);
      S.insert(i);
    } else {
      split(u[i], v[i]);
      int j = pos[v[i]];
      if (w[j] > w[i]) {
        cut(u[j], j);
        cut(v[j], j);
        link(u[i], i);
        link(v[i], i);
        S.erase(j);
        S.insert(i);
      }
    }
    if (cnt)
      puts("-1");
    else
      while (!S.empty()) {
        int x = *S.rbegin();
        cut(u[x], x);
        cut(v[x], x);
        if (getsize(u[x]) & 1) {
          link(u[x], x);
          link(v[x], x);
          printf("%d\n", w[x]);
          break;
        } else
          S.erase(x);
      }
  }
  return 0;
}
