#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005, MAXM = 300005;
int n, m, ans[MAXM];
struct graph {
  int tim;
  int cnt, fa[MAXN], siz[MAXN];
  int tx[MAXN], ty[MAXN];
  void first() {
    for (int i = 1; i <= n; i++) fa[i] = i;
    for (int i = 1; i <= n; i++) siz[i] = 1;
    cnt = n >> 1, tim = 0;
  }
  void cancel() {
    int x = tx[tim], y = ty[tim];
    --tim;
    fa[x] = x;
    siz[y] -= siz[x];
    if ((siz[x] & 1) && (siz[y] & 1)) ++cnt;
  }
  int get(int x) { return x == fa[x] ? x : get(fa[x]); }
  void merge(int x, int y) {
    int fx = get(x), fy = get(y);
    if (fx == fy) return;
    if ((siz[fx] & 1) && (siz[fy] & 1)) --cnt;
    if (siz[fx] > siz[fy]) swap(fx, fy);
    fa[fx] = fy;
    siz[fy] += siz[fx];
    ++tim, tx[tim] = fx, ty[tim] = fy;
  }
} G;
struct edge {
  int x, y, v;
} e[MAXM];
int rk[MAXM], w[MAXM];
bool cmp(const int &aa, const int &bb) {
  if (e[aa].v != e[bb].v) return e[aa].v < e[bb].v;
  return aa < bb;
}
void solve(int l, int r, int vl, int vr) {
  if (l > r) return;
  int mid = (l + r) >> 1, bg_tim = G.tim;
  for (int i = l; i <= mid; i++)
    if (rk[i] < vl) G.merge(e[i].x, e[i].y);
  for (int i = vl; i <= vr && G.cnt; i++) {
    if (w[i] <= mid) G.merge(e[w[i]].x, e[w[i]].y);
    if (!G.cnt) ans[mid] = i;
  }
  while (G.tim > bg_tim) G.cancel();
  if (ans[mid]) {
    for (int i = vl; i < ans[mid]; i++)
      if (w[i] < l) G.merge(e[w[i]].x, e[w[i]].y);
    solve(l, mid - 1, ans[mid], vr);
    while (G.tim > bg_tim) G.cancel();
  }
  for (int i = l; i <= mid; i++)
    if (rk[i] < vl) G.merge(e[i].x, e[i].y);
  solve(mid + 1, r, vl, ans[mid] ? ans[mid] : vr);
  while (G.tim > bg_tim) G.cancel();
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].v);
  if (n & 1) {
    for (int i = 1; i <= m; i++) puts("-1");
    return 0;
  }
  for (int i = 1; i <= m; i++) w[i] = i;
  sort(w + 1, w + m + 1, cmp);
  for (int i = 1; i <= m; i++) rk[w[i]] = i;
  G.first();
  solve(1, m, 1, m);
  for (int i = 1; i <= m; i++)
    if (ans[i])
      printf("%d\n", e[w[ans[i]]].v);
    else
      puts("-1");
  return 0;
}
