#include <bits/stdc++.h>
using namespace std;
const int Maxn = 100000;
const int Maxm = 300000;
struct LinkCutTree {
  struct Node {
    int key, id;
    Node *mx;
    bool is_node;
    int siz, siz2;
    Node *fa, *ch[2];
    bool rev;
  };
  Node pool[Maxn + Maxm + 5];
  Node *ncnt, *NIL;
  void init() {
    ncnt = NIL = &pool[0];
    NIL->fa = NIL->ch[0] = NIL->ch[1] = NIL->mx = NIL;
    NIL->rev = false;
    NIL->siz = NIL->siz2 = 0;
    NIL->key = -1, NIL->is_node = false;
  }
  Node *newnode(int k, int id, int typ) {
    Node *p = ++ncnt;
    p->key = k, p->id = id, p->siz = typ, p->siz2 = 0;
    p->is_node = typ;
    p->fa = p->ch[0] = p->ch[1] = NIL;
    p->mx = p;
    return p;
  }
  void pushup(Node *rt) {
    if (rt->key > rt->ch[0]->mx->key && rt->key > rt->ch[1]->mx->key)
      rt->mx = rt;
    else if (rt->ch[0]->mx->key > rt->ch[1]->mx->key)
      rt->mx = rt->ch[0]->mx;
    else
      rt->mx = rt->ch[1]->mx;
    rt->siz = rt->is_node + rt->ch[0]->siz + rt->ch[1]->siz + rt->siz2;
  }
  void pushdown(Node *rt) {
    if (rt->rev) {
      if (rt->ch[0] != NIL) rt->ch[0]->rev ^= 1;
      if (rt->ch[1] != NIL) rt->ch[1]->rev ^= 1;
      swap(rt->ch[0], rt->ch[1]);
      rt->rev = false;
    }
  }
  bool dir(Node *rt) { return rt->fa->ch[1] == rt; }
  bool isroot(Node *rt) {
    return rt->fa == NIL || (rt->fa->ch[0] != rt && rt->fa->ch[1] != rt);
  }
  void setchild(Node *rt, Node *x, int d) {
    rt->ch[d] = x;
    if (x != NIL) x->fa = rt;
  }
  void rotate(Node *x) {
    Node *y = x->fa;
    pushdown(y), pushdown(x);
    int d = dir(x);
    if (isroot(y))
      x->fa = y->fa;
    else
      setchild(y->fa, x, dir(y));
    setchild(y, x->ch[!d], d);
    setchild(x, y, !d);
    pushup(y);
  }
  void splay(Node *x) {
    pushdown(x);
    while (!isroot(x)) {
      Node *y = x->fa;
      if (isroot(y))
        rotate(x);
      else {
        if (dir(y) == dir(x))
          rotate(y);
        else
          rotate(x);
        rotate(x);
      }
    }
    pushup(x);
  }
  void access(Node *x) {
    Node *c = NIL;
    while (x != NIL) {
      splay(x);
      x->siz2 += x->ch[1]->siz - c->siz;
      setchild(x, c, 1);
      pushup(x);
      c = x, x = x->fa;
    }
  }
  void makeroot(Node *x) {
    access(x), splay(x);
    x->rev ^= 1;
  }
  Node *findroot(Node *x) {
    access(x), splay(x);
    Node *y = x;
    while (y->ch[0] != NIL) y = y->ch[0];
    pushup(y);
    return y;
  }
  void link(Node *x, Node *y) {
    makeroot(x), makeroot(y);
    x->fa = y;
    y->siz2 += x->siz;
  }
  void cut(Node *x, Node *y) {
    makeroot(x);
    access(y), splay(y);
    y->ch[0] = NIL, x->fa = NIL;
    pushup(y);
  }
  Node *lca(Node *x, Node *y) {
    access(x), splay(x);
    splay(y);
    while (y->fa != NIL) {
      y = y->fa;
      splay(y);
    }
    return y;
  }
  Node *query(Node *x, Node *y) {
    Node *z = lca(x, y);
    Node *ret = z;
    access(x), splay(z);
    if (z->ch[1]->mx->key > ret->key) ret = z->ch[1]->mx;
    access(y), splay(z);
    if (z->ch[1]->mx->key > ret->key) ret = z->ch[1]->mx;
    return ret;
  }
};
struct Edge {
  int u, v, w, id;
  bool operator<(const Edge &rhs) const {
    return w == rhs.w ? id < rhs.id : w < rhs.w;
  }
};
LinkCutTree::Node *nd[Maxn + Maxm + 5];
LinkCutTree tr;
set<Edge> edges;
Edge e[Maxm + 5];
int N, M;
int main() {
  tr.init();
  scanf("%d %d", &N, &M);
  for (int i = 1; i <= N; i++) nd[i] = tr.newnode(-1, -1, 1);
  int tot = N;
  for (int i = 1; i <= M; i++) {
    scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].w);
    e[i].id = i;
    edges.insert(e[i]);
    nd[N + i] = tr.newnode(e[i].w, i, 0);
    if (tr.findroot(nd[e[i].u]) == tr.findroot(nd[e[i].v])) {
      LinkCutTree::Node *p = tr.query(nd[e[i].u], nd[e[i].v]);
      tr.splay(p);
      if (p->key > e[i].w) {
        edges.erase(e[p->id]);
        tr.cut(p, nd[e[p->id].u]), tr.cut(p, nd[e[p->id].v]);
        tr.link(nd[e[i].u], nd[N + i]), tr.link(nd[e[i].v], nd[N + i]);
      } else
        edges.erase(e[i]);
    } else {
      tr.makeroot(nd[e[i].u]), tr.makeroot(nd[e[i].v]);
      if (nd[e[i].u]->siz % 2 == 1 && nd[e[i].v]->siz % 2 == 1) tot -= 2;
      tr.link(nd[e[i].u], nd[N + i]), tr.link(nd[e[i].v], nd[N + i]);
    }
    if (tot == 0) {
      set<Edge>::iterator it = edges.end();
      it--;
      while (true) {
        LinkCutTree::Node *p = nd[N + it->id];
        LinkCutTree::Node *q = nd[e[it->id].u], *r = nd[e[it->id].v];
        tr.makeroot(p), tr.access(q), tr.access(r);
        if (q->siz & 1) break;
        tr.access(q);
        if (r->siz & 1) break;
        edges.erase(it);
        it = edges.end();
        it--;
      }
      it = edges.end();
      it--;
      printf("%d\n", it->w);
    } else
      puts("-1");
  }
  return 0;
}
