#include <bits/stdc++.h>
using namespace std;
inline int qr() {
  int res = 0, f = 0, c = getchar();
  while (!isdigit(c)) f |= c == 45, c = getchar();
  while (isdigit(c)) res = res * 10 + c - 48, c = getchar();
  return f ? -res : res;
}
const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
int r[maxn], siz[maxn], stk[maxn], top, n, m, cnt;
int Find(int x) { return x == r[x] ? x : Find(r[x]); }
int merge(int x, int y) {
  x = Find(x), y = Find(y);
  if (x == y) return 0;
  if (siz[x] > siz[y]) swap(x, y);
  cnt -= siz[x] & 1;
  cnt -= siz[y] & 1;
  r[x] = y;
  siz[y] += siz[x];
  stk[++top] = x;
  cnt += siz[y] & 1;
  return 1;
}
void undo(int x = 1e8) {
  while (x--) {
    int u = stk[top], v = r[u];
    cnt -= siz[v] & 1;
    siz[v] -= siz[u];
    cnt += siz[v] & 1;
    cnt += siz[u] & 1;
    r[u] = u;
    top--;
  }
}
int ans[maxn * 3];
struct E {
  int u, v, w;
} e[maxn * 3];
map<int, vector<int>> C;
void solve(int tl, int tr, int vl, int vr) {
  if (vl > vr || tl > tr) return;
  int base = 0, backup1 = 0, res = vr, mid = (tl + tr) / 2;
  for (int i = tl; i <= mid; i++)
    if (e[i].w < vl) base += merge(e[i].u, e[i].v);
  for (auto it = C.lower_bound(vl), ed = C.upper_bound(vr); it != ed; it++)
    for (int tim : it->second)
      if (tim <= mid) {
        if (cnt) backup1 += merge(e[tim].u, e[tim].v);
        if (!cnt) {
          res = e[tim].w;
          goto end;
        }
      }
end:;
  ans[mid] = res;
  undo(backup1);
  solve(mid + 1, tr, vl, res);
  undo(base);
  base = 0;
  for (auto it = C.lower_bound(vl), ed = C.lower_bound(res); it != ed; it++)
    for (int tim : it->second)
      if (tim < tl) base += merge(e[tim].u, e[tim].v);
  solve(tl, mid - 1, res, vr);
  undo(base);
}
int main() {
  memset(ans, 0x3f, sizeof ans);
  n = qr(), m = qr();
  for (int t = 1; t <= n; ++t) r[t] = t, siz[t] = 1;
  cnt = n;
  for (int t = 1; t <= m; ++t)
    e[t].u = qr(), e[t].v = qr(), e[t].w = qr(), C[e[t].w].push_back(t);
  solve(1, m, 1, inf);
  for (int t = 1; t <= m; ++t) printf("%d\n", ans[t] == inf ? -1 : ans[t]);
  return 0;
}
