#include <bits/stdc++.h>
using namespace std;
const int N = 400010;
int a[N], b[N], c[N];
bool del[N];
priority_queue<pair<int, int> > q;
struct node *null;
struct node {
  node *ch[2], *f;
  bool rev;
  pair<int, int> mx, w;
  int sum, s;
  bool c() { return f == null || (f->ch[0] != this && f->ch[1] != this); }
  int d() { return f->ch[1] == this; }
  void st(node *c, int d) { ch[d] = c, c->f = this; }
  void upd() {
    mx = max(ch[0]->mx, ch[1]->mx);
    mx = max(mx, w);
    sum = ch[0]->sum ^ ch[1]->sum ^ s;
  }
  void push() {
    if (rev) swap(ch[0], ch[1]);
    ch[0]->rev ^= rev, ch[1]->rev ^= rev, rev = 0;
  }
} T[N];
void rot(node *o) {
  node *f = o->f;
  int d = o->d();
  if (!f->c())
    f->f->st(o, f->d());
  else
    o->f = f->f;
  f->st(o->ch[d ^ 1], d), o->st(f, d ^ 1);
  f->upd();
}
void fix(node *o) {
  if (!o->c()) fix(o->f);
  o->push();
}
void splay(node *o) {
  fix(o);
  while (!o->c()) {
    if (!o->f->c()) (o->d() == o->f->d() ? rot(o->f) : rot(o));
    rot(o);
  }
  o->upd();
}
void access(node *o) {
  node *t = o;
  for (node *c = null; o != null; o = o->f) {
    splay(o);
    o->s ^= o->ch[1]->sum ^ c->sum;
    o->st(c, 1), (c = o)->upd();
  }
  splay(t);
}
node *frt(node *o) {
  access(o);
  while (o->push(), o->ch[0] != null) o = o->ch[0];
  splay(o);
  return o;
}
void mrt(node *o) {
  access(o);
  o->rev ^= 1;
}
bool debug = false;
void link(node *u, node *v) {
  mrt(u);
  access(v), v->s ^= u->sum, v->upd(), u->f = v;
}
void cut(node *u, node *v) {
  mrt(u);
  access(v);
  v->ch[0] = u->f = null;
  v->upd();
}
int n, m;
void init() {
  null = &T[0];
  null->ch[0] = null->ch[1] = null->f = null;
  null->rev = 0, null->mx = null->w = make_pair(0, 0);
  for (int i = 1; i <= n + m; i++) T[i] = *null;
  for (int i = 1; i <= n; i++) T[i].sum = T[i].s = 1;
}
bool check(int e) {
  cut(T + a[e], T + n + e), cut(T + b[e], T + n + e);
  mrt(T + a[e]), mrt(T + b[e]);
  if (!T[a[e]].sum && !T[b[e]].sum) return true;
  link(T + a[e], T + n + e);
  link(T + b[e], T + n + e);
  return false;
}
int main() {
  scanf("%d%d", &n, &m);
  if (n & 1) {
    for (int i = 1; i <= m; i++) printf("-1\n");
  } else {
    init();
    int cnt = n;
    for (int i = 1; i <= m; i++) {
      scanf("%d%d%d", &a[i], &b[i], &c[i]);
      T[n + i].w = T[n + i].mx = make_pair(c[i], i);
      q.push(T[n + i].w);
      if (frt(T + a[i]) != frt(T + b[i])) {
        mrt(T + a[i]), mrt(T + b[i]);
        if (T[a[i]].sum && T[b[i]].sum) cnt -= 2;
        link(T + a[i], T + n + i);
        link(T + b[i], T + n + i);
        mrt(T + b[i]);
      } else {
        mrt(T + a[i]), access(T + b[i]);
        if (T[b[i]].mx.first > c[i]) {
          int e = T[b[i]].mx.second;
          del[e] = true;
          cut(T + a[e], T + n + e);
          cut(T + b[e], T + n + e);
          link(T + a[i], T + n + i);
          link(T + b[i], T + n + i);
        } else
          del[i] = true;
      }
      if (cnt)
        printf("-1\n");
      else {
        bool flag = false;
        while (del[q.top().second] || check(q.top().second)) q.pop();
        printf("%d\n", q.top().first);
      }
    }
  }
  return 0;
}
