#include <bits/stdc++.h>
using namespace std;
int read() {
  int ret = 0;
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  while (c >= '0' && c <= '9') {
    ret = ret * 10 + c - '0';
    c = getchar();
  }
  return ret;
}
const int MAXN = 100000, MAXM = 300000;
pair<int, int> e[MAXN + 10];
int Q[MAXN + 10];
int rub[MAXN + 10], rtop;
int n, odd;
struct Heap {
  int q[MAXN + 10], pos[MAXN + 10], back[MAXN + 10];
  int cnt;
  void sw(int l, int r) {
    swap(q[l], q[r]);
    swap(back[l], back[r]);
    pos[back[l]] = l;
    pos[back[r]] = r;
    return;
  }
  void up(int first) {
    while ((first >> 1) && q[first] > q[first >> 1]) {
      sw(first, first >> 1);
      first = first >> 1;
    }
    return;
  }
  void down(int first) {
    while ((first << 1) <= cnt) {
      int loc = ((first << 1) == cnt || q[first << 1] > q[first << 1 | 1])
                    ? (first << 1)
                    : (first << 1 | 1);
      if (q[loc] > q[first]) {
        sw(first, loc);
        first = loc;
      } else
        break;
    }
    return;
  }
  void add(int first, int k) {
    q[++cnt] = k;
    pos[first] = cnt;
    back[cnt] = first;
    up(cnt);
    return;
  }
  void del(int first) {
    int loc = pos[first];
    sw(loc, cnt);
    cnt--;
    down(loc);
    up(loc);
    return;
  }
} h;
struct LCT {
  int q[(MAXN << 1) + 10], MAX[(MAXN << 1) + 10];
  int sz[(MAXN << 1) + 10], nsz[(MAXN << 1) + 10];
  int f[(MAXN << 1) + 10], ch[(MAXN << 1) + 10][2], rev[(MAXN << 1) + 10];
  void set(int first, int l, int c) {
    ch[first][l] = c;
    if (c) f[c] = first;
    return;
  }
  void update(int first) {
    MAX[first] = first;
    sz[first] = nsz[first] + (first <= n);
    for (int i = 0; i < 2; i++) {
      if (ch[first][i] && q[MAX[ch[first][i]]] > q[MAX[first]])
        MAX[first] = MAX[ch[first][i]];
      sz[first] += sz[ch[first][i]];
    }
    return;
  }
  void rotate(int first) {
    int fa = f[first], ff = f[fa], l = (ch[f[first]][1] == first);
    if (!((ch[f[fa]][0] != fa) && (ch[f[fa]][1] != fa)))
      set(ff, (ch[f[fa]][1] == fa), first);
    f[first] = ff;
    set(fa, l, ch[first][l ^ 1]);
    set(first, l ^ 1, fa);
    update(fa);
    return;
  }
  void down(int first) {
    if (rev[first]) {
      for (int i = 0; i < 2; i++)
        if (ch[first][i]) rev[ch[first][i]] ^= 1;
      swap(ch[first][0], ch[first][1]);
      rev[first] = 0;
    }
    return;
  }
  void splay(int first) {
    static int st[MAXN + 10];
    int top = 0;
    st[++top] = first;
    for (int px = first; !((ch[f[px]][0] != px) && (ch[f[px]][1] != px));
         px = f[px])
      st[++top] = f[px];
    for (; top; top--) down(st[top]);
    for (; !((ch[f[first]][0] != first) && (ch[f[first]][1] != first));) {
      int fa = f[first];
      if (!((ch[f[fa]][0] != fa) && (ch[f[fa]][1] != fa))) {
        if ((ch[f[first]][1] == first) == (ch[f[fa]][1] == fa))
          rotate(fa);
        else
          rotate(first);
      }
      rotate(first);
    }
    update(first);
    return;
  }
  void access(int first) {
    splay(first);
    nsz[first] += sz[ch[first][1]];
    ch[first][1] = 0;
    for (; f[first];) {
      int u = f[first];
      splay(u);
      nsz[u] -= sz[first];
      nsz[u] += sz[ch[u][1]];
      set(u, 1, first);
      update(u);
      splay(first = u);
    }
    return;
  }
  void beroot(int first) {
    access(first);
    splay(first);
    rev[first] ^= 1;
    return;
  }
  int root(int first) {
    access(first);
    splay(first);
    while (ch[first][0]) first = ch[first][0];
    return first;
  }
  void cut(int loc, int first, int second) {
    beroot(loc);
    h.del(loc - n);
    rub[++rtop] = loc;
    access(first);
    splay(loc);
    f[ch[loc][1]] = 0;
    ch[loc][1] = 0;
    access(second);
    splay(loc);
    f[ch[loc][1]] = 0;
    ch[loc][1] = 0;
    access(first);
    splay(first);
    access(second);
    splay(second);
    if ((sz[first] & 1) && (sz[second] & 1)) odd += 2;
    return;
  }
  void link(int loc, int first, int second, int k) {
    access(first);
    splay(first);
    access(second);
    splay(second);
    if ((sz[first] & 1) && (sz[second] & 1)) odd -= 2;
    q[loc] = k;
    e[loc - n] = make_pair(first, second);
    h.add(loc - n, k);
    beroot(first);
    f[first] = loc;
    nsz[loc] += sz[first];
    beroot(second);
    f[second] = loc;
    nsz[loc] += sz[second];
    return;
  }
  void check(int first, int second, int k) {
    if (root(first) != root(second)) {
      beroot(first);
      beroot(second);
      link(rub[rtop--], first, second, k);
    } else {
      beroot(first);
      access(second);
      splay(second);
      int loc = MAX[second];
      if (q[loc] > k) {
        cut(loc, first, second);
        link(rub[rtop--], first, second, k);
      }
    }
    return;
  }
} tr;
int m;
int ans() {
  int loc;
  while (odd == 0) {
    loc = h.back[1];
    tr.cut(loc + n, e[loc].first, e[loc].second);
  }
  tr.link(rub[rtop--], e[loc].first, e[loc].second, h.q[h.pos[loc]]);
  return h.q[h.pos[loc]];
}
void init() {
  int r = 0;
  odd = n = read();
  m = read();
  for (int i = 1; i <= n; i++) {
    rub[++rtop] = n + i;
    tr.sz[i] = 1;
  }
  for (int i = 1; i <= m; i++) {
    int first = read(), second = read(), k = read();
    tr.check(first, second, k);
    if (odd == 0)
      printf("%d\n", ans());
    else
      puts("-1");
  }
  return;
}
int main() {
  init();
  return 0;
}
