#include <bits/stdc++.h>
template <typename T>
inline void read(T &x) {
  x = 0;
  char c = getchar();
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
}
using namespace std;
int n, m;
struct Edge {
  int u, v, w, id;
  Edge() {}
  Edge(int jzp, int zzz, int lsr, int chr) {
    u = jzp, v = zzz, w = lsr, id = chr;
  }
} E[301000], Ev[301000];
bool cmp(const Edge &a, const Edge &b) { return a.w < b.w; }
int stkx[301000], stky[301000], stop, siz[301000], oddcnt, fath[301000];
int find(int cur) { return fath[cur] == cur ? cur : find(fath[cur]); }
inline void merge(int x, int y) {
  x = find(x), y = find(y);
  if (x == y) return;
  if (siz[x] > siz[y]) swap(x, y);
  stkx[++stop] = x, stky[stop] = y;
  if (siz[x] & 1) --oddcnt;
  if (siz[y] & 1) --oddcnt;
  siz[y] += siz[x];
  fath[x] = y;
  if (siz[y] & 1) ++oddcnt;
}
inline void cancl(int x, int y) {
  fath[x] = x;
  if (siz[y] & 1) --oddcnt;
  siz[y] -= siz[x];
  if (siz[x] & 1) ++oddcnt;
  if (siz[y] & 1) ++oddcnt;
}
int st[301000], ed[301000];
int ans[301000];
int h[301000], ltot;
void sol(int l, int r, int d, int u) {
  if (l > r) return;
  int memo = stop, mid = (l + r) >> 1;
  for (int i = l; i <= mid; ++i) {
    if (E[i].w <= d) merge(E[i].u, E[i].v);
  }
  int memo_ = stop;
  if (!oddcnt) {
    ans[mid] = d;
  } else {
    for (int i = d + 1; i <= u; ++i) {
      for (int j = st[i]; j <= ed[i]; ++j) {
        if (Ev[j].id <= mid) merge(Ev[j].u, Ev[j].v);
      }
      if (!oddcnt) {
        ans[mid] = i;
        break;
      }
    }
  }
  if (!ans[mid]) {
    while (stop != memo_) cancl(stkx[stop], stky[stop]), --stop;
    sol(mid + 1, r, d, u);
    while (stop != memo) cancl(stkx[stop], stky[stop]), --stop;
    return;
  } else {
    while (stop != memo_) cancl(stkx[stop], stky[stop]), --stop;
    sol(mid + 1, r, d, ans[mid]);
    while (stop != memo) cancl(stkx[stop], stky[stop]), --stop;
    for (int i = d + 1; i <= ans[mid]; ++i) {
      for (int j = st[i]; j <= ed[i]; ++j) {
        if (Ev[j].id < l) merge(Ev[j].u, Ev[j].v);
      }
    }
    sol(l, mid - 1, ans[mid], u);
    while (stop != memo) cancl(stkx[stop], stky[stop]), --stop;
    return;
  }
}
int main() {
  read(n), read(m);
  for (int i = 1; i <= n; ++i) siz[i] = 1, fath[i] = i;
  oddcnt = n;
  for (int i = 1; i <= m; ++i) {
    int u, v, w;
    read(u), read(v), read(w);
    h[i] = w;
    Ev[i] = Edge(u, v, w, i);
  }
  sort(h + 1, h + 1 + m);
  ltot = unique(h + 1, h + 1 + m) - h - 1;
  for (int i = 1; i <= m; ++i)
    Ev[i].w = lower_bound(h + 1, h + 1 + ltot, Ev[i].w) - h;
  memcpy(E, Ev, sizeof(Ev));
  sort(Ev + 1, Ev + 1 + m, cmp);
  for (int i = 1; i <= m; ++i)
    if (Ev[i].w != Ev[i - 1].w) ed[Ev[i - 1].w] = i - 1, st[Ev[i].w] = i;
  ed[Ev[m].w] = m;
  sol(1, m, 1, ltot);
  h[0] = -1;
  for (int i = 1; i <= m; ++i) printf("%d\n", h[ans[i]]);
  return 0;
}
