#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int res = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -f;
    ch = getchar();
  }
  while (isdigit(ch)) {
    res = (res << 3) + (res << 1) + ch - '0';
    ch = getchar();
  }
  return res * f;
}
namespace qiqi {
const int N = 6e5 + 5;
int n, m, siz[N], si[N], fa[N], ch[N][2], rev[N], cnt, mx[N];
bool del[N];
struct Edge {
  int u, v, w, id;
  inline bool friend operator<(Edge a, Edge b) { return a.w < b.w; }
} e[N];
priority_queue<Edge> q;
inline bool isroot(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; }
inline void push_up(int x) {
  siz[x] = (x <= n) + si[x] + siz[ch[x][0]] + siz[ch[x][1]];
  mx[x] = x > n ? x - n : 0;
  for (int i = (0); i <= (1); ++i)
    if (e[mx[ch[x][i]]].w > e[mx[x]].w) mx[x] = mx[ch[x][i]];
}
inline void push_down(int x, bool k) {
  if (k && !isroot(x)) push_down(fa[x], k);
  if (rev[x]) {
    swap(ch[x][0], ch[x][1]);
    rev[ch[x][0]] ^= 1;
    rev[ch[x][1]] ^= 1;
    rev[x] = 0;
  }
}
inline void rotate(int x) {
  int y = fa[x], z = fa[y], l = x == ch[y][1];
  if (!isroot(y)) ch[z][y == ch[z][1]] = x;
  fa[x] = z;
  push_up(fa[ch[y][l] = ch[x][l ^ 1]] = y);
  push_up(fa[ch[x][l ^ 1] = y] = x);
}
inline void splay(int x) {
  push_down(x, 1);
  while (!isroot(x)) {
    int y = fa[x];
    if (!isroot(y)) rotate(x ^ y ^ ch[y][0] ^ ch[fa[y]][0] ? x : y);
    rotate(x);
  }
  push_up(x);
}
inline void access(int x) {
  for (int i = 0; x; x = fa[i = x]) {
    splay(x);
    si[x] += siz[ch[x][1]];
    si[x] -= siz[ch[x][1] = i];
    push_up(x);
  }
}
inline void makeroot(int x) {
  access(x);
  splay(x);
  rev[x] ^= 1;
}
inline void split(int x, int y) {
  makeroot(x);
  access(y);
  splay(y);
}
inline int find(int x) {
  access(x);
  splay(x);
  while (ch[x][rev[x]]) x = ch[x][rev[x]];
  splay(x);
  return x;
}
inline int qry(int x, int y) {
  split(x, y);
  return mx[y];
}
inline void cut(int x, int y) {
  split(x, y);
  cnt -= siz[y] & 1;
  ch[y][0] = fa[x] = 0;
  push_up(y);
  cnt += (siz[x] & 1) + (siz[y] & 1);
}
inline void link(int x, int y) {
  split(x, y);
  cnt -= (siz[x] & 1) + (siz[y] & 1);
  si[fa[x] = y] += siz[x];
  push_up(y);
  cnt += siz[y] & 1;
}
inline int upd(int x, int y, int id) {
  bool flg = 1;
  if (find(x) == find(y)) {
    int k = qry(x, y);
    if (e[k].w > e[id].w) {
      del[k] = 1;
      cut(e[k].u, k + n);
      cut(e[k].v, k + n);
    } else
      flg = 0;
  }
  if (flg) {
    link(x, id + n);
    link(y, id + n);
  }
  if (cnt) return -1;
  while (1) {
    Edge e = q.top();
    q.pop();
    if (del[e.id]) continue;
    cut(e.u, e.id + n);
    cut(e.v, e.id + n);
    if (cnt) {
      q.push(e);
      link(e.u, e.id + n);
      link(e.v, e.id + n);
      return q.top().w;
    }
  }
}
void main() {
  e[0].w = 0;
  cnt = n = read();
  m = read();
  for (int i = (1); i <= (m); ++i) {
    q.push(e[i] = (Edge){read(), read(), read(), i});
    printf("%d\n", upd(e[i].u, e[i].v, i));
  }
}
}  // namespace qiqi
int main() {
  qiqi::main();
  return 0;
}
