#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
const int MAXN = 600010;
const int INF = 1e9;
struct data {
  int u, v, id, w;
} E[MAXN + 1], F[MAXN + 1];
int N, M;
bool cmpw(data a, data b) { return a.w < b.w; }
bool In[MAXN + 1];
int sum = 0;
int now = 1;
int ans[MAXN + 1];
namespace LCT {
int id[MAXN + 1];
bool tag[MAXN + 1];
int mx[MAXN + 1];
int ch[MAXN + 1][2];
int fa[MAXN + 1];
int si[MAXN + 1], val[MAXN + 1];
int sta[MAXN + 1];
inline void Con(int s, int f, int x) {
  ch[f][x] = s;
  fa[s] = f;
  return;
}
inline bool chd(int x) { return (ch[fa[x]][1] == x); }
inline bool nroot(int x) { return (ch[fa[x]][0] == x || ch[fa[x]][1] == x); }
inline void update(int x) {
  val[x] = val[ch[x][0]] ^ val[ch[x][1]] ^ (x <= N) ^ si[x];
  mx[x] = max(id[x], max(mx[ch[x][0]], mx[ch[x][1]]));
  return;
}
inline void rotate(int x) {
  int y = fa[x], z = fa[y], cx = chd(x), cy = chd(y), b = ch[x][cx ^ 1];
  if (nroot(y))
    Con(x, z, cy);
  else
    fa[x] = z;
  Con(y, x, cx ^ 1);
  if (b)
    Con(b, y, cx);
  else
    ch[y][cx] = 0;
  update(y);
  update(x);
  return;
}
inline void Color(int x) {
  if (!x) return;
  tag[x] ^= 1;
  swap(ch[x][0], ch[x][1]);
}
inline void pushdown(int x) {
  if (tag[x]) Color(ch[x][0]), Color(ch[x][1]), tag[x] = 0;
  return;
}
inline void Splay(int x) {
  int top = 0, y = x;
  sta[++top] = y;
  while (nroot(y)) sta[++top] = y = fa[y];
  while (top) pushdown(sta[top]), top--;
  while (nroot(x)) {
    y = fa[x];
    if (nroot(y)) rotate((chd(x) ^ chd(y)) ? x : y);
    rotate(x);
  }
  return;
}
inline void access(int x) {
  for (int y = 0; x; y = x, x = fa[x]) {
    Splay(x);
    if (y) {
      si[x] ^= val[y];
    }
    if (ch[x][1]) {
      si[x] ^= val[ch[x][1]];
    }
    ch[x][1] = y;
    update(x);
  }
  return;
}
inline void chroot(int x) {
  access(x);
  Splay(x);
  Color(x);
}
inline int findroot(int x) {
  access(x);
  Splay(x);
  while (ch[x][0]) x = ch[x][0];
  Splay(x);
  return x;
}
inline void _Link(int x, int y) {
  chroot(x);
  chroot(y);
  fa[x] = y;
  si[y] ^= val[x];
  update(y);
  return;
}
inline void _Cut(int x, int y) {
  chroot(x);
  if (findroot(y) == x && !ch[y][0] && fa[y] == x)
    ch[x][1] = 0, fa[y] = 0, update(x);
  return;
}
inline void Cut(int x, int y, int d) {
  if (!In[d]) return;
  In[d] = 0;
  chroot(x);
  sum -= val[x];
  _Cut(x, d + N);
  _Cut(y, d + N);
  chroot(x);
  sum += val[x];
  chroot(y);
  sum += val[y];
}
inline void Link(int x, int y, int d) {
  if (findroot(x) == findroot(y)) {
    chroot(y);
    access(x);
    Splay(x);
    if (mx[x] < d) return;
    Cut(F[mx[x]].u, F[mx[x]].v, mx[x]);
  }
  id[d + N] = d;
  mx[d + N] = d;
  val[d + N] = 0;
  In[d] = 1;
  chroot(x);
  sum -= val[x];
  chroot(y);
  sum -= val[y];
  _Link(x, d + N);
  _Link(y, d + N);
  chroot(x);
  sum += val[x];
  return;
}
}  // namespace LCT
using namespace LCT;
int main() {
  N = read(), M = read();
  for (int i = 1; i <= M; i++) {
    int u = read(), v = read(), w = read();
    E[i].u = u;
    E[i].v = v;
    E[i].id = i;
    E[i].w = w;
    F[i] = E[i];
  }
  for (int i = 1; i <= N; i++)
    fa[i] = 0, val[i] = 1, mx[i] = -INF, id[i] = -INF;
  sort(E + 1, E + M + 1, cmpw);
  sum = N;
  while (sum && now <= M) {
    Link(E[now].u, E[now].v, E[now].id);
    ++now;
  }
  if (sum) {
    for (int i = 1; i <= M; i++) puts("-1");
    return 0;
  }
  ans[M] = E[now - 1].w;
  for (int i = M; i >= 1; i--) {
    if (F[i].w <= E[now - 1].w) {
      Cut(F[i].u, F[i].v, i);
    }
    while (sum && now <= M) {
      if (E[now].id < i) Link(E[now].u, E[now].v, E[now].id);
      ++now;
    }
    if (!sum)
      ans[i - 1] = E[now - 1].w;
    else
      ans[i - 1] = -1;
  }
  for (int i = 1; i <= M; i++) printf("%d\n", ans[i]);
  return 0;
}
