#include <bits/stdc++.h>
using namespace std;
int read() {
  int s = 0, t = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') t = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
  return s * t;
}
int n, m;
struct dsu {
  int odd, sz[100100], fa[100100], stk[100100], top, dl[100100];
  void init() {
    for (int i = 1; i <= n; i++) sz[i] = 1, fa[i] = i;
    odd = n;
    top = 0;
  }
  int find(int x) { return fa[x] == x ? x : find(fa[x]); }
  void merge(int x, int y) {
    int fx = find(x), fy = find(y);
    if (fx == fy) return;
    if (sz[fx] > sz[fy]) swap(fx, fy);
    stk[++top] = fx;
    fa[fx] = fy;
    if ((sz[fx] & 1) && (sz[fy] & 1))
      odd -= 2, dl[top] = 2;
    else
      dl[top] = 0;
    sz[fy] += sz[fx];
  }
  void del() {
    odd += dl[top];
    int x = stk[top--];
    sz[fa[x]] -= sz[x];
    fa[x] = x;
  }
  bool ok() { return !odd; }
} d;
int ans[300100];
struct edge {
  int x, y, v, t;
} e[300100], p[300100];
void solve(int l, int r, int vl, int vr) {
  if (l > r) return;
  int mid = l + r >> 1, lst = d.top, mans = -1;
  for (int i = l; i <= mid; i++)
    if (e[i].v < vl) d.merge(e[i].x, e[i].y);
  for (int i = vl; i <= vr; i++) {
    if (p[i].t <= mid) d.merge(p[i].x, p[i].y);
    if (d.ok()) {
      mans = i;
      break;
    }
  }
  while (d.top > lst) d.del();
  if (mans == -1) {
    for (int i = l; i <= mid; i++) ans[i] = -1;
    for (int i = l; i <= mid; i++)
      if (e[i].v < vl) d.merge(e[i].x, e[i].y);
    solve(mid + 1, r, vl, vr);
    while (d.top > lst) d.del();
    return;
  }
  ans[mid] = p[mans].v;
  for (int i = l; i <= mid; i++)
    if (e[i].v < vl) d.merge(e[i].x, e[i].y);
  solve(mid + 1, r, vl, mans);
  while (d.top > lst) d.del();
  for (int i = vl; i < mans; i++)
    if (p[i].t < l) d.merge(p[i].x, p[i].y);
  solve(l, mid - 1, mans, vr);
  while (d.top > lst) d.del();
}
bool operator<(edge a, edge b) { return a.v == b.v ? a.t < b.t : a.v < b.v; }
int main() {
  n = read(), m = read();
  d.init();
  for (int i = 1; i <= m; i++) {
    int x = read(), y = read(), v = read();
    e[i] = p[i] = (edge){x, y, v, i};
  }
  sort(p + 1, p + m + 1);
  for (int i = 1; i <= m; i++) e[p[i].t].v = i;
  solve(1, m, 1, m);
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
  return 0;
}
