#include <bits/stdc++.h>
using namespace std;
inline int qr() {
  int ret = 0, f = 0, c = getchar();
  while (!isdigit(c)) f |= c == 45, c = getchar();
  while (isdigit(c)) ret = ret * 10 + c - 48, c = getchar();
  return f ? -ret : ret;
}
const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
int r[maxn], siz[maxn], stk[maxn], top, n, m, cnt_odd;
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_odd -= siz[x] & 1;
  cnt_odd -= siz[y] & 1;
  r[x] = y;
  siz[y] += siz[x];
  stk[++top] = x;
  cnt_odd += siz[y] & 1;
  return 1;
}
void Undo(int x = 1e8) {
  while (x--) {
    int u = stk[top], v = r[u];
    cnt_odd -= siz[v] & 1;
    siz[v] -= siz[u];
    cnt_odd += siz[v] & 1;
    cnt_odd += 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 l, int r, int L, int R) {
  if (L > R) return;
  int base = 0, sav = 0, sav1 = 0, ret = R;
  for (int t = l; t <= ((l + r) >> 1); ++t)
    if (e[t].w < L) base += Merge(e[t].u, e[t].v);
  for (auto it = C.lower_bound(L), ed = C.upper_bound(R); it != ed; ++it)
    for (auto t : it->second)
      if (t <= ((l + r) >> 1)) {
        if (cnt_odd) sav += Merge(e[t].u, e[t].v);
        if (!cnt_odd) {
          ret = e[t].w;
          it = prev(ed);
          break;
        }
      }
  ans[((l + r) >> 1)] = ret;
  Undo(sav);
  if (((l + r) >> 1) < r) solve(((l + r) >> 1) + 1, r, L, ret);
  Undo(base);
  for (auto it = C.lower_bound(L), ed = C.lower_bound(ret); it != ed; ++it)
    for (auto t : it->second)
      if (t < l) sav1 += Merge(e[t].u, e[t].v);
  if (l < ((l + r) >> 1)) solve(l, ((l + r) >> 1) - 1, ret, R);
  Undo(sav1);
}
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_odd = 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;
}
