#include <bits/stdc++.h>
using namespace std;
const int MN = 5e5 + 5;
struct node {
  int u, v, w, home;
} E[MN], e[MN];
int N, M, ans[MN], home[MN];
inline bool cmp(node a, node b) { return a.w < b.w; }
struct DSU {
  int fa[MN], sz[MN], tot, top, st[MN];
  inline void build() {
    tot = N;
    for (int i = 1; i <= N; ++i) fa[i] = i, sz[i] = 1;
  }
  inline int find(int x) {
    while (x != fa[x]) x = fa[x];
    return x;
  }
  inline void link(int u, int v) {
    u = find(u), v = find(v);
    if (u == v) return;
    if (sz[u] < sz[v]) swap(u, v);
    fa[v] = u, tot -= (sz[u] & 1), tot -= (sz[v] & 1);
    sz[u] += sz[v];
    tot += (sz[u] & 1);
    st[++top] = v;
  }
  inline void undo() {
    int v = st[top--], u = fa[v];
    tot -= (sz[u] & 1);
    sz[u] -= sz[v];
    fa[v] = v;
    tot += (sz[u] & 1);
    tot += (sz[v] & 1);
    return;
  }
} S;
inline void CDQ(int l, int r, int ql, int qr) {
  if (l > r) return;
  int mid = l + r >> 1;
  int lst = S.top;
  for (int i = l; i <= mid; ++i)
    if (home[i] < ql) S.link(E[i].u, E[i].v);
  for (int i = ql; i <= qr; ++i) {
    if (e[i].home <= mid) S.link(e[i].u, e[i].v);
    if (!S.tot) {
      ans[mid] = i;
      break;
    }
  }
  while (S.top > lst) S.undo();
  if (!ans[mid]) {
    for (int i = l; i <= mid; ++i)
      if (home[i] < ql) S.link(E[i].u, E[i].v);
    CDQ(mid + 1, r, ql, qr);
    while (S.top > lst) S.undo();
    return;
  }
  for (int i = l; i <= mid; ++i)
    if (home[i] < ql) S.link(E[i].u, E[i].v);
  CDQ(mid + 1, r, ql, ans[mid]);
  while (S.top > lst) S.undo();
  for (int i = ql; i < ans[mid]; ++i)
    if (e[i].home < l) S.link(e[i].u, e[i].v);
  CDQ(l, mid - 1, ans[mid], qr);
  while (S.top > lst) S.undo();
}
int main() {
  scanf("%d%d", &N, &M);
  S.build();
  for (int i = 1; i <= M; ++i) {
    scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
    e[i].home = i;
  }
  for (int i = 1; i <= M; ++i) E[i] = e[i];
  sort(e + 1, e + 1 + M, cmp);
  for (int i = 1; i <= M; ++i) home[e[i].home] = i;
  CDQ(1, M, 1, M);
  for (int i = 1; i <= M; ++i)
    if (!ans[i])
      printf("-1\n");
    else
      printf("%d\n", e[ans[i]].w);
  return 0;
}
