#include <bits/stdc++.h>
using namespace std;
int n, m;
int u[500005], v[500005], w[500005];
bool vis[500005];
priority_queue<pair<int, int> > Q;
struct LCT {
  int fa[500005], sz[500005], si[500005], ch[500005][2], tag[500005],
      st[500005], mx[500005], val[500005];
  bool unroot(int x) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; }
  bool get(int x) { return ch[fa[x]][1] == x; }
  void push_rev(int x) {
    swap(ch[x][0], ch[x][1]);
    tag[x] ^= 1;
  }
  void push_up(int x) {
    sz[x] = sz[ch[x][0]] ^ sz[ch[x][1]] ^ si[x] ^ (x <= n);
    mx[x] = x;
    if (val[mx[ch[x][0]]] > val[mx[x]]) mx[x] = mx[ch[x][0]];
    if (val[mx[ch[x][1]]] > val[mx[x]]) mx[x] = mx[ch[x][1]];
    return;
  }
  void push_down(int x) {
    if (!tag[x]) return;
    tag[x] ^= 1;
    if (ch[x][0]) push_rev(ch[x][0]);
    if (ch[x][1]) push_rev(ch[x][1]);
    return;
  }
  void rotate(int x) {
    int y = fa[x], z = fa[y], tx = get(x), ty = get(y), p = ch[x][tx ^ 1];
    if (unroot(y)) ch[z][ty] = x;
    fa[x] = z;
    if (p) fa[p] = y;
    ch[y][tx] = p;
    ch[x][tx ^ 1] = y;
    fa[y] = x;
    push_up(y);
    return;
  }
  void splay(int x) {
    int top = 0, u = x;
    st[++top] = u;
    while (unroot(u)) st[++top] = u = fa[u];
    while (top) push_down(st[top--]);
    while (unroot(x)) {
      int y = fa[x];
      if (unroot(y)) rotate(get(x) == get(y) ? y : x);
      rotate(x);
    }
    push_up(x);
    return;
  }
  void access(int x) {
    for (int y = 0; x; x = fa[y = x])
      splay(x), si[x] ^= sz[ch[x][1]] ^ sz[y], ch[x][1] = y, push_up(x);
    return;
  }
  int find_root(int x) {
    access(x);
    splay(x);
    while (ch[x][0]) push_down(x), x = ch[x][0];
    return splay(x), x;
  }
  void make_root(int x) {
    access(x);
    splay(x);
    push_rev(x);
  }
  void split(int x, int y) {
    make_root(x);
    access(y);
    splay(y);
  }
  void link(int x, int y) {
    make_root(x);
    fa[x] = y;
    si[y] ^= sz[x];
    push_up(y);
  }
  void cut(int x, int y) {
    split(x, y);
    fa[x] = ch[y][0] = 0;
    push_up(y);
  }
  int query(int x) {
    make_root(x);
    return sz[x];
  }
} T;
int main() {
  scanf("%d%d", &n, &m);
  int cnt = n;
  if (n & 1) {
    while (m--) puts("-1");
    return 0;
  }
  for (int i = 1; i <= n; ++i) T.sz[i] = 1;
  for (int i = 1; i <= m; ++i) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    u[i] = x;
    v[i] = y;
    w[i] = z;
    if (T.find_root(x) == T.find_root(y)) {
      T.split(x, y);
      int id = T.mx[y] - n;
      if (w[id] > z) {
        T.cut(u[id], id + n), T.cut(v[id], id + n);
        T.val[i + n] = z;
        T.link(x, i + n), T.link(y, i + n);
        Q.push(make_pair(z, i));
        vis[id] = true;
      }
    } else {
      if (T.query(x) && T.query(y)) cnt -= 2;
      T.val[i + n] = z;
      T.link(x, i + n), T.link(y, i + n);
      Q.push(make_pair(z, i));
    }
    if (cnt)
      puts("-1");
    else {
      while (!Q.empty()) {
        int id = Q.top().second;
        Q.pop();
        if (vis[id]) continue;
        T.cut(u[id], id + n);
        T.cut(v[id], id + n);
        if (T.query(u[id]) || T.query(v[id])) {
          T.link(u[id], id + n);
          T.link(v[id], id + n);
          Q.push(make_pair(w[id], id));
          break;
        }
      }
      printf("%d\n", Q.top().first);
    }
  }
  return 0;
}
