#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
using namespace std;
namespace ywy {
inline int get() {
  int n = 0;
  char c;
  while ((c = getchar()) || 23333) {
    if (c >= '0' && c <= '9') break;
    if (c == '-') goto s;
  }
  n = c - '0';
  while ((c = getchar()) || 23333) {
    if (c >= '0' && c <= '9')
      n = n * 10 + c - '0';
    else
      return (n);
  }
s:
  while ((c = getchar()) || 23333) {
    if (c >= '0' && c <= '9')
      n = n * 10 - c + '0';
    else
      return (n);
  }
}
inline int get1() {
  int n = 0;
  char c;
  while ((c = getchar()) || 23333) {
    if (c >= '0' && c <= '9') break;
    if (c == '-') goto s;
  }
  n = c - '0';
  while ((c = getchar()) || 23333) {
    if (c >= '0' && c <= '9')
      n = n * 10 + c - '0';
    else
      return (n);
  }
s:
  while ((c = getchar()) || 23333) {
    if (c >= '0' && c <= '9')
      n = n * 10 - c + '0';
    else
      return (n);
  }
}
void print(int num) {
  if (num >= 10) print(num / 10);
  putchar(num % 10 + '0');
}
int fa[400001], minpos[400001], val[400001], ch[400001][2], size[400001];
int stk[400001];
unsigned char bj[400001];
int sz[400001];
inline void up(int tree) {
  size[tree] = size[ch[tree][0]] + size[ch[tree][1]] + sz[tree];
  minpos[tree] = tree;
  int minn = val[tree];
  if (val[minpos[ch[tree][0]]] > minn)
    minn = val[minpos[ch[tree][0]]], minpos[tree] = minpos[ch[tree][0]];
  if (val[minpos[ch[tree][1]]] > minn)
    minn = val[minpos[ch[tree][1]]], minpos[tree] = minpos[ch[tree][1]];
}
inline void down(int tree) {
  if (bj[tree])
    bj[tree] = 0, swap(ch[tree][0], ch[tree][1]), bj[ch[tree][0]] ^= 1,
    bj[ch[tree][1]] ^= 1;
}
inline void xuan(int me) {
  int tree = fa[me], cjr = fa[tree];
  down(cjr);
  down(tree);
  down(me);
  int op = (me == ch[fa[me]][1]), ls = ch[me][op ^ 1];
  ch[me][op ^ 1] = tree;
  ch[tree][op] = ls;
  fa[ls] = tree;
  if (!(tree != ch[fa[tree]][0] && tree != ch[fa[tree]][1]))
    ch[cjr][(tree == ch[fa[tree]][1])] = me;
  fa[tree] = me;
  fa[me] = cjr;
  up(tree);
  up(me);
}
inline void splay(int tree) {
  if ((tree != ch[fa[tree]][0] && tree != ch[fa[tree]][1])) {
    down(tree);
    return;
  }
  int sp = 0, tmp = tree;
  while (!(tmp != ch[fa[tmp]][0] && tmp != ch[fa[tmp]][1]))
    stk[sp] = tmp, sp++, tmp = fa[tmp];
  stk[sp] = tmp;
  sp++;
  while (sp) sp--, down(stk[sp]);
  while (!(tree != ch[fa[tree]][0] && tree != ch[fa[tree]][1])) {
    int cjr = fa[tree];
    if (!(cjr != ch[fa[cjr]][0] && cjr != ch[fa[cjr]][1]))
      xuan(((cjr == ch[fa[cjr]][1]) == (tree == ch[fa[tree]][1])) ? cjr : tree);
    xuan(tree);
  }
  up(tree);
}
inline void access(int tree) {
  splay(tree);
  down(tree);
  sz[tree] += size[ch[tree][1]];
  ch[tree][1] = 0;
  up(tree);
  while (fa[tree]) {
    int cjr = fa[tree];
    splay(cjr);
    down(cjr);
    sz[cjr] -= size[tree];
    sz[cjr] += size[ch[cjr][1]];
    ch[cjr][1] = tree;
    up(cjr);
    splay(tree);
  }
  up(tree);
}
inline void makeroot(int tree) {
  access(tree);
  splay(tree);
  bj[tree] ^= 1;
}
inline void link(int a, int b) {
  makeroot(a);
  makeroot(b);
  sz[a] += size[b];
  fa[b] = a;
  up(a);
}
inline void cut(int a, int b) {
  makeroot(a);
  access(b);
  splay(a);
  down(a);
  ch[a][1] = fa[b] = 0;
  up(a);
}
inline int findroot(int tree) {
  access(tree);
  splay(tree);
  down(tree);
  while (ch[tree][0]) tree = ch[tree][0], down(tree);
  splay(tree);
  return (tree);
}
typedef struct _b {
  int s;
  int t;
  int l;
  friend bool operator<(const _b &a, const _b &b) { return (a.l < b.l); }
} bian;
bian memchi[300001];
int gnn = 1;
int ss[400001], ts[400001];
typedef struct _n {
  int l;
  int pt;
  friend bool operator<(const _n &a, const _n &b) {
    if (a.l == b.l) return (a.pt < b.pt);
    return (a.l < b.l);
  }
  friend bool operator==(const _n &a, const _n &b) { return (a.pt == b.pt); }
} node;
priority_queue<node> que, del;
void ywymain() {
  int n = get(), m = get();
  if (n & 1) {
    for (register int i = 1; i <= m; i++) printf("-1\n");
    return;
  }
  for (register int i = 1; i <= n; i++) {
    sz[i] = size[i] = 1;
    minpos[i] = i;
  }
  int cnt = n;
  int gnn = n + 1;
  while (m) {
    m--;
    int s = get(), t = get(), l = get();
    int aa = findroot(s), ab = findroot(t);
    if (aa != ab) {
      if (size[aa] & 1) cnt--;
      if (size[ab] & 1) cnt--;
      if ((size[ab] + size[aa]) & 1) cnt++;
      int me = gnn;
      gnn++;
      node cjr;
      cjr.pt = me;
      cjr.l = l;
      que.push(cjr);
      ss[me] = s;
      ts[me] = t;
      val[me] = l;
      minpos[me] = me;
      link(me, s);
      link(me, t);
    } else {
      makeroot(s);
      access(t);
      splay(t);
      if (val[minpos[t]] > l) {
        int dst = minpos[t];
        node cjr;
        cjr.pt = dst;
        cjr.l = val[dst];
        del.push(cjr);
        cut(ss[dst], dst);
        cut(ts[dst], dst);
        int me = gnn;
        gnn++;
        ss[me] = s;
        ts[me] = t;
        val[me] = l;
        minpos[me] = me;
        link(me, s);
        link(me, t);
        cjr.pt = me;
        cjr.l = l;
        que.push(cjr);
      }
    }
    if (cnt) {
      printf("-1\n");
    } else {
      while (1) {
        while (!que.empty() && !del.empty() && que.top() == del.top())
          que.pop(), del.pop();
        int pt = que.top().pt;
        makeroot(pt);
        access(ss[pt]);
        splay(ss[pt]);
        if (sz[ss[pt]] & 1) break;
        cut(ss[pt], pt);
        cut(ts[pt], pt);
        que.pop();
      }
      print(que.top().l);
      putchar('\n');
    }
  }
}
}  // namespace ywy
int main() {
  ywy::ywymain();
  return (0);
}
