#include <bits/stdc++.h>
const int xo = 400001, xn = 100001, xm = 300001;
int U[xm], V[xm], W[xm], N, M;
int son[xo][2], fa[xo], rev[xo], wmax[xo], wme[xo], size[xo], sub[xo], single;
struct ecmp {
  inline bool operator()(int x, int y) {
    return W[x] > W[y] || (W[x] == W[y] && x < y);
  }
};
inline int maxe(int x, int y) { return ecmp()(x, y) ? x : y; }
inline void up(int p) {
  wmax[p] = maxe(maxe(wmax[son[p][0]], wmax[son[p][1]]), wme[p]);
  size[p] = size[son[p][0]] + size[son[p][1]] + sub[p];
}
inline void down(int p) {
  if (rev[p]) {
    std::swap(son[p][0], son[p][1]);
    rev[son[p][0]] ^= 1;
    rev[son[p][1]] ^= 1;
    rev[p] = 0;
  }
}
inline int isroot(int p) { return son[fa[p]][0] != p && son[fa[p]][1] != p; }
inline void rotate(int p) {
  int f = fa[p], ff = fa[f], w = son[f][1] == p;
  if (!isroot(f)) son[ff][son[ff][1] == f] = p;
  fa[p] = ff;
  fa[f] = p;
  fa[son[p][w ^ 1]] = f;
  son[f][w] = son[p][w ^ 1];
  son[p][w ^ 1] = f;
  up(f);
  up(p);
}
void splay(int p) {
  static int q[xo];
  int D = 1;
  q[1] = p;
  while (!isroot(q[D])) {
    q[D + 1] = fa[q[D]];
    D++;
  }
  while (D) down(q[D--]);
  while (!isroot(p)) {
    if (!isroot(fa[p]) && ((son[fa[fa[p]]][1] == fa[p]) ^ (son[fa[p]][0] == p)))
      rotate(fa[p]);
    rotate(p);
  }
}
void access(int p) {
  int t = 0;
  while (p) {
    splay(p);
    sub[p] += size[son[p][1]] - size[t];
    son[p][1] = t;
    up(p);
    t = p;
    p = fa[p];
  }
}
void makeroot(int p) {
  access(p);
  splay(p);
  rev[p] ^= 1;
}
int findroot(int p) {
  access(p);
  splay(p);
  int res = p;
  while (down(res), son[res][0]) res = son[res][0];
  splay(res);
  return res;
}
int query(int u, int v) {
  makeroot(u);
  access(v);
  splay(v);
  return v;
}
void link(int u, int v) {
  makeroot(v);
  splay(v);
  fa[v] = u;
  splay(u);
  sub[u] += size[v];
  size[u] += size[v];
}
void cut(int u, int v) {
  makeroot(u);
  access(v);
  splay(u);
  son[u][1] = 0;
  fa[v] = 0;
  up(u);
}
std::set<int, ecmp> S;
int wholesize(int x) {
  makeroot(x);
  splay(x);
  return size[x];
}
int rm(int i) {
  single -= wholesize(U[i]) & 1;
  cut(N + i, U[i]);
  cut(N + i, V[i]);
  single += (wholesize(U[i]) & 1) + (wholesize(V[i]) & 1);
  if (single) {
    single -= (wholesize(U[i]) & 1) + (wholesize(V[i]) & 1);
    link(N + i, U[i]);
    link(N + i, V[i]);
    single += wholesize(U[i]) & 1;
    return 0;
  }
  S.erase(i);
  return 1;
}
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= M; i++) {
    scanf("%d%d%d", U + i, V + i, W + i);
    wmax[N + i] = wme[N + i] = i;
  }
  for (int i = 1; i <= N; i++) size[i] = sub[i] = 1;
  single = N;
  for (int i = 1; i <= M; i++) {
    int fu = findroot(U[i]), fv = findroot(V[i]);
    if (fu == fv) {
      int wx = wmax[query(U[i], V[i])];
      if (maxe(wx, i) == i) {
        printf("%d\n", single ? -1 : W[*S.begin()]);
        continue;
      }
      single -= wholesize(U[i]) & 1;
      cut(N + wx, U[wx]);
      cut(N + wx, V[wx]);
      single += (wholesize(U[i]) & 1) + (wholesize(V[i]) & 1);
      S.erase(wx);
    }
    single -= (wholesize(U[i]) & 1) + (wholesize(V[i]) & 1);
    link(N + i, U[i]);
    link(N + i, V[i]);
    single += wholesize(U[i]) & 1;
    S.insert(i);
    while (!single && !S.empty() && rm(*S.begin()))
      ;
    printf("%d\n", single ? -1 : W[*S.begin()]);
  }
  return 0;
}
