#include <bits/stdc++.h>
using namespace std;
inline int get() {
  char c;
  while (!isdigit(c = getchar()))
    ;
  int v = c - 48;
  while (isdigit(c = getchar())) v = v * 10 + c - 48;
  return v;
}
const int maxn = 100005, maxm = 300005;
struct info {
  int u, v, w, i;
  bool operator<(const info &rhs) const {
    if (w != rhs.w) return w > rhs.w;
    return i < rhs.i;
  }
} e[maxm];
int n, m, odd;
struct node {
  node *ch[2], *fa, *pf;
  bool flip;
  int s, r, d, v, md, mv;
  void up() {
    s = ch[0]->s + ch[1]->s + r;
    mv = v;
    md = d;
    if (ch[0]->mv > mv) mv = ch[0]->mv, md = ch[0]->md;
    if (ch[1]->mv > mv) mv = ch[1]->mv, md = ch[1]->md;
  }
  void rev() {
    flip ^= 1;
    swap(ch[0], ch[1]);
  }
  void down() {
    if (flip) {
      ch[0]->rev();
      ch[1]->rev();
      flip = 0;
    }
  }
} po[maxn + maxm], *nul;
class Functional_Link_Cut_Tree {
  void rot(node *o, int d) {
    node *k = o->fa;
    o->pf = k->pf;
    k->down();
    o->down();
    (k->fa->ch[k->fa->ch[1] == k] = o)->fa = k->fa;
    (k->ch[d ^ 1] = o->ch[d])->fa = k;
    (o->ch[d] = k)->fa = o;
    k->up();
    o->up();
  }
  void splay(node *o) {
    for (node *t = o->fa; t != nul; t = o->fa)
      if (t->fa == nul)
        rot(o, t->ch[0] == o);
      else {
        t->fa->down();
        int d = t->fa->ch[1] == t;
        d == (t->ch[1] == o) ? (rot(t, d ^ 1), rot(o, d ^ 1))
                             : (rot(o, d), rot(o, d ^ 1));
      }
    o->down();
    o->up();
  }
  void pu(node *o, node *t = nul) {
    splay(o);
    o->r += o->ch[1]->s;
    o->r -= t->s;
    o->ch[1]->fa = nul;
    o->ch[1]->pf = o;
    ((o->ch[1] = t)->fa = o)->up();
  }
  node *access(node *o) {
    for (pu(o); o->pf; o = o->pf) pu(o->pf, o);
    return o;
  }
  node *find_pa(node *o) {
    access(o);
    splay(o);
    for ((o = o->ch[0])->down(); o->ch[1] != nul; (o = o->ch[1])->down())
      ;
    return o;
  }
  node *find_root(node *o) {
    access(o);
    splay(o);
    for (; o->ch[0] != nul; o = o->ch[0])
      ;
    return o;
  }
  void make_root(node *o) {
    access(o)->rev();
    splay(o);
  }
  void link(node *x, node *y) {
    make_root(x);
    access(y);
    x->pf = y;
    y->r += x->s;
    splay(y);
  }
  void cut(node *o) {
    pu(find_pa(o));
    o->pf = 0;
  }
  int qry(node *x, node *y) {
    make_root(x);
    return access(y)->md;
  }

 public:
  void init() {
    nul = po;
    nul->fa = nul->ch[0] = nul->ch[1] = nul;
    nul->pf = 0;
    nul->s = nul->flip = 0;
    nul->mv = nul->v = -999999999;
  }
  void link(int x, int y) { link(&po[x], &po[y]); }
  void cut(int x, int y) {
    make_root(&po[x]);
    cut(&po[y]);
  }
  int qry(int x, int y) { return qry(&po[x], &po[y]); }
  bool check(int x, int y) { return find_root(&po[x]) == find_root(&po[y]); }
  int size(int x) {
    make_root(&po[x]);
    return po[x].s;
  }
} tr;
set<info> s;
void add_edge(int x, int y, int z, int id) {
  po[id + n].ch[0] = po[id + n].ch[1] = po[id + n].fa = nul;
  po[id + n].pf = 0;
  po[id + n].s = 0;
  po[id + n].flip = 0;
  po[id + n].d = po[id + n].md = id + n;
  po[id + n].v = po[id + n].mv = z;
  if (tr.check(x, y)) {
    int t = tr.qry(x, y);
    if (po[t].v <= z) return;
    tr.cut(t, e[t - n].u);
    tr.cut(t, e[t - n].v);
    s.erase(e[t - n]);
    tr.link(id + n, x);
    tr.link(id + n, y);
    s.insert(e[id]);
    return;
  }
  if ((tr.size(x) & 1) && (tr.size(y) & 1)) odd -= 2;
  tr.link(id + n, x);
  tr.link(id + n, y);
  s.insert(e[id]);
}
int solve() {
  if (odd) return -1;
  while (!odd && s.size()) {
    set<info>::iterator it = s.begin();
    int x = it->u, y = it->v, id = it->i;
    tr.cut(id + n, x);
    tr.cut(id + n, y);
    odd += (tr.size(x) & 1) + (tr.size(y) & 1);
    if (!odd) s.erase(it);
  }
  set<info>::iterator it = s.begin();
  int x = it->u, y = it->v, id = it->i;
  tr.link(id + n, x);
  tr.link(id + n, y);
  odd = 0;
  return it->w;
}
int main() {
  n = get();
  m = get();
  tr.init();
  for (int i = 1; i <= n; ++i) {
    po[i].fa = po[i].ch[0] = po[i].ch[1] = nul;
    po[i].pf = 0;
    po[i].s = po[i].r = 1;
    po[i].flip = 0;
    po[i].d = po[i].md = i;
    po[i].v = po[i].mv = -999999999;
  }
  odd = n;
  for (int i = 1; i <= m; ++i) {
    e[i].u = get();
    e[i].v = get();
    e[i].w = get();
    e[i].i = i;
    add_edge(e[i].u, e[i].v, e[i].w, i);
    printf("%d\n", solve());
  }
  return 0;
}
