#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
const int maxm = 200000 + 10;
struct Edge {
  int u, v;
  Edge() {}
  Edge(int u, int v) : u(u), v(v) {}
} e[maxn];
int n, odd, m;
int cnt[maxn], tot;
struct Heap {
  int q[maxn], num[maxn], back[maxn];
  int sz;
  inline void ex(int l, int r) {
    swap(q[l], q[r]);
    swap(back[l], back[r]);
    num[back[l]] = l;
    num[back[r]] = r;
  }
  inline void up(int now) {
    int next;
    while (now > 1) {
      next = now >> 1;
      if (q[now] <= q[next]) break;
      ex(now, next);
      now = next;
    }
  }
  inline void down(int now) {
    while ((now << 1) <= sz) {
      int next = now << 1;
      if (next < sz && q[next + 1] > q[next]) next++;
      if (q[now] >= q[next]) break;
      ex(now, next);
      now = next;
    }
  }
  inline void add(int x, int v) {
    q[++sz] = v;
    num[x] = sz;
    back[sz] = x;
    up(sz);
  }
  inline void del(int x) {
    int pos = num[x];
    ex(pos, sz--);
    down(pos), up(pos);
  }
} h;
struct LCT {
  int fa[maxm], ch[maxm][2];
  int len[maxm], maxx[maxm];
  int sz[maxm], vsz[maxm];
  int anc[maxn], top;
  bool rev[maxm];
  inline void push_up(int x) {
    maxx[x] = x;
    sz[x] = vsz[x] + (x <= n);
    for (int i = 0; i < 2; i++)
      if (ch[x][i] && len[maxx[ch[x][i]]] > len[maxx[x]])
        maxx[x] = maxx[ch[x][i]];
    sz[x] += sz[ch[x][0]] + sz[ch[x][1]];
  }
  inline void push_down(int x) {
    if (rev[x]) {
      swap(ch[x][0], ch[x][1]);
      rev[ch[x][0]] ^= 1;
      rev[ch[x][1]] ^= 1;
      rev[x] = 0;
    }
  }
  inline bool root(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; }
  inline void Rotate(int x) {
    int y = fa[x], z = fa[y];
    int l = ch[y][1] == x, r = l ^ 1;
    if (!root(y)) ch[z][ch[z][1] == y] = x;
    fa[x] = z;
    fa[y] = x;
    fa[ch[x][r]] = y;
    ch[y][l] = ch[x][r];
    ch[x][r] = y;
    push_up(y);
  }
  inline void Splay(int x) {
    anc[top = 1] = x;
    for (int i = x; !root(i); i = fa[i]) anc[++top] = fa[i];
    while (top) push_down(anc[top--]);
    while (!root(x)) {
      int y = fa[x];
      if (!root(y)) {
        if ((ch[fa[x]][1] == x) ^ (ch[fa[y]][1] == y))
          Rotate(x);
        else
          Rotate(y);
      }
      Rotate(x);
    }
    push_up(x);
  }
  inline void Access(int x) {
    for (int t = 0; x; t = x, x = fa[x]) {
      Splay(x);
      vsz[x] -= sz[t];
      vsz[x] += sz[ch[x][1]];
      ch[x][1] = t;
      push_up(x);
    }
  }
  inline void Beroot(int x) {
    Access(x);
    Splay(x);
    rev[x] ^= 1;
  }
  inline int find_root(int x) {
    Access(x);
    Splay(x);
    while (ch[x][0]) x = ch[x][0];
    return x;
  }
  inline void Link(int pos, int x, int y, int k) {
    Access(x);
    Splay(x);
    Access(y);
    Splay(y);
    if ((sz[x] & 1) && (sz[y] & 1)) odd -= 2;
    len[pos] = k;
    e[pos - n] = Edge(x, y);
    h.add(pos - n, k);
    Beroot(x);
    fa[x] = pos;
    vsz[pos] += sz[x];
    Beroot(y);
    fa[y] = pos;
    vsz[pos] += sz[y];
  }
  inline void Cut(int pos, int x, int y) {
    Beroot(pos);
    h.del(pos - n);
    cnt[++tot] = pos;
    Access(x);
    Splay(pos);
    fa[ch[pos][1]] = 0;
    ch[pos][1] = 0;
    Access(y);
    Splay(pos);
    fa[ch[pos][1]] = 0;
    ch[pos][1] = 0;
    Access(x);
    Splay(x);
    Access(y);
    Splay(y);
    if ((sz[x] & 1) && (sz[y] & 1)) odd += 2;
  }
  inline void check(int x, int y, int k) {
    if (find_root(x) != find_root(y)) {
      Beroot(x);
      Beroot(y);
      Link(cnt[tot--], x, y, k);
    } else {
      Beroot(x);
      Access(y);
      Splay(y);
      int pos = maxx[y];
      if (len[pos] > k) {
        Cut(pos, x, y);
        Link(cnt[tot--], x, y, k);
      }
    }
  }
} Gintoki;
inline int read() {
  int x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x;
}
inline int solve() {
  int pos;
  while (!odd) {
    pos = h.back[1];
    Gintoki.Cut(pos + n, e[pos].u, e[pos].v);
  }
  Gintoki.Link(cnt[tot--], e[pos].u, e[pos].v, h.q[h.num[pos]]);
  return h.q[1];
}
int main() {
  if (fopen("in.in", "r") != NULL) {
    freopen("in.in", "r", stdin);
    freopen("out.out", "w", stdout);
  }
  odd = n = read();
  m = read();
  for (int i = 1; i <= n; i++) {
    cnt[++tot] = n + i;
    Gintoki.sz[i] = 1;
  }
  for (int i = 1, u, v, w; i <= m; i++) {
    u = read(), v = read(), w = read();
    Gintoki.check(u, v, w);
    if (!odd)
      printf("%d\n", solve());
    else
      puts("-1");
  }
  return 0;
}
