#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int nm = 0;
  bool fh = true;
  char cw = getchar();
  for (; !isdigit(cw); cw = getchar()) fh ^= (cw == '-');
  for (; isdigit(cw); cw = getchar()) nm = nm * 10 + (cw - '0');
  return fh ? nm : -nm;
}
int n, m, hv, ans[300200];
struct Edge {
  int x, y, w, tim;
  inline void gtin(int t) { x = read(), y = read(), w = read(), tim = t; }
  inline bool operator<(const Edge b) const { return w > b.w; }
} e[300200], nd[300200];
priority_queue<Edge> Q;
namespace DSU {
int fd[300200], sz[300200], sta[300200], top;
inline void init() {
  hv = n;
  for (int i = 1; i <= n; i++) fd[i] = i, sz[i] = 1;
}
inline int Fd(int x) { return (fd[x] == x) ? x : Fd(fd[x]); }
inline void mg(int fx, int fy) {
  if (sz[fx] < sz[fy]) swap(fx, fy);
  hv -= (sz[fx] & 1), hv -= (sz[fy] & 1), hv += (sz[fx] + sz[fy]) & 1;
  fd[fy] = fx, sta[++top] = fy, sz[fx] += sz[fy];
}
inline void Back(int x) {
  hv -= (sz[fd[x]] & 1), sz[fd[x]] -= sz[x];
  hv += (sz[fd[x]] & 1), fd[x] = x, hv += (sz[x] & 1);
}
}  // namespace DSU
using namespace DSU;
vector<Edge> vec[300200 << 2];
inline void ins(int x, int l, int r, int L, int R, Edge v) {
  if (L > R) return;
  int mid = (l + r) >> 1;
  if (L <= l && r <= R) {
    vec[x].push_back(v);
    return;
  }
  if (L <= mid) ins(x << 1, l, mid, L, R, v);
  if (R > mid) ins(x << 1 | 1, mid + 1, r, L, R, v);
}
inline void solve(int x, int l, int r, int mx) {
  int mid = (l + r) >> 1, tms = top, cnt = 0;
  for (int i = 0, TP = vec[x].size(); i < TP; i++) {
    int fx = Fd(vec[x][i].x), fy = Fd(vec[x][i].y);
    if (fx == fy) continue;
    mg(fx, fy), mx = max(mx, vec[x][i].w);
  }
  if (l == r) {
    while (hv && !Q.empty()) {
      Edge nw = Q.top();
      Q.pop();
      if (nw.tim <= l) {
        int fx = Fd(nw.x), fy = Fd(nw.y);
        if (fx ^ fy) mg(fx, fy), mx = max(mx, nw.w);
        nd[++cnt] = nw;
      }
    }
    if (!hv) {
      ans[l] = mx;
      for (int i = 1; i <= cnt; i++) ins(1, 1, m, nd[i].tim, l - 1, nd[i]);
    } else
      ans[l] = -1;
  } else
    solve(x << 1 | 1, mid + 1, r, mx), solve(x << 1, l, mid, mx);
  while (top > tms) Back(sta[top--]);
}
int main() {
  n = read(), m = read();
  for (int i = 1; i <= m; i++) e[i].gtin(i), Q.push(e[i]);
  init(), solve(1, 1, m, -1);
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
  return 0;
}
