#include <bits/stdc++.h>
using namespace std;
inline int read() {
  register int x = 0;
  register char ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = getchar();
  return x;
}
const int N = 1e5 + 5;
const int M = 3e5 + 5;
struct each {
  int x, y, z, t;
  bool operator<(const each k) const { return z > k.z; }
} a[M], st[N];
vector<each> b[M << 2];
priority_queue<each> que;
int n, m, top, cnt, fa[N], si[N], dep[N], ans[M];
inline int find(int x) {
  while (fa[x]) x = fa[x];
  return x;
}
inline void merge(int x, int y) {
  x = find(x), y = find(y);
  if (x == y) return;
  cnt -= (si[x] & 1) + (si[y] & 1);
  if (dep[x] > dep[y]) swap(x, y);
  if (dep[x] == dep[y]) st[top].y = 1, ++dep[y];
  st[++top].x = x, fa[x] = y, si[y] += si[x], cnt += si[y] & 1;
}
inline void del(each k) {
  int x = k.x, y = fa[x];
  if (k.y) dep[y]--;
  cnt -= si[y] & 1, si[y] -= si[x];
  cnt += (si[x] & 1) + (si[y] & 1), fa[x] = 0;
}
void add(int l, int r, int x, int y, each z, int rt) {
  if (x > y) return;
  if (x <= l && y >= r) {
    b[rt].push_back(z);
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid) add(l, mid, x, y, z, rt << 1);
  if (y > mid) add(++mid, r, x, y, z, rt << 1 | 1);
}
void dfs(int l, int r, int rt, int res) {
  int tmp = top, si = b[rt].size();
  for (register int i = 0; i < si; i++) {
    merge(b[rt][i].x, b[rt][i].y);
    res = max(res, b[rt][i].z);
  }
  if (l == r) {
    int num = 0;
    while (cnt && !que.empty()) {
      each k = que.top();
      que.pop();
      if (k.t <= l) {
        merge(k.x, k.y), res = max(res, k.z);
        a[++num] = k;
      }
    }
    if (!cnt) {
      for (register int i = 1; i <= num; i++) add(1, m, a[i].t, l - 1, a[i], 1);
      ans[l] = res;
    } else
      ans[l] = -1;
  } else {
    int mid = (l + r) >> 1;
    dfs(mid + 1, r, rt << 1 | 1, res), dfs(l, mid, rt << 1, res);
  }
  while (top != tmp) del(st[top--]);
}
int main() {
  n = read(), m = read();
  for (register int i = 1; i <= n; i++) si[i] = 1;
  for (register int i = 1; i <= m; i++) {
    a[i].x = read(), a[i].y = read();
    a[i].z = read(), a[i].t = i, que.push(a[i]);
  }
  cnt = n, dfs(1, m, 1, -1);
  for (register int i = 1; i <= m; i++) printf("%d\n", ans[i]);
  return 0;
}
