#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int nOdd;
struct LCT {
  int pre[N], ch[N][2], bef[N];
  int sz[N], sum[N], rev[N];
  int mx[N], f[N], w[N];
  inline void Rev(int x) {
    if (!x) return;
    swap(ch[x][0], ch[x][1]);
    rev[x] ^= 1;
  }
  inline void push_down(int x) {
    if (!rev[x]) return;
    Rev(ch[x][0]);
    Rev(ch[x][1]);
    rev[x] = 0;
  }
  inline void upd(int &x, int &f, int v, int who) {
    if (x >= v) return;
    x = v;
    f = who;
  }
  inline void push_up(int x) {
    sum[x] = sz[x] + sum[ch[x][0]] + sum[ch[x][1]];
    mx[x] = 0;
    upd(mx[x], f[x], mx[ch[x][0]], f[ch[x][0]]);
    upd(mx[x], f[x], mx[ch[x][1]], f[ch[x][1]]);
    upd(mx[x], f[x], w[x], x);
  }
  inline void P(int x) {
    if (pre[x]) P(pre[x]);
    push_down(x);
  }
  inline void Rotate(int x, bool kind) {
    int y = pre[x];
    ch[y][!kind] = ch[x][kind];
    pre[ch[x][kind]] = y;
    pre[x] = pre[y];
    if (pre[x]) ch[pre[x]][ch[pre[x]][1] == y] = x;
    ch[x][kind] = y;
    pre[y] = x;
    push_up(y);
  }
  inline void Splay(int x) {
    P(x);
    int rt = x;
    for (; pre[rt]; rt = pre[rt])
      ;
    if (rt ^ x) {
      bef[x] = bef[rt];
      bef[rt] = 0;
      while (pre[x]) Rotate(x, ch[pre[x]][0] == x);
    }
    push_up(x);
  }
  inline void Access(int x) {
    int fa = 0;
    for (; x; x = bef[x]) {
      Splay(x);
      sz[x] += sum[ch[x][1]];
      sz[x] -= sum[fa];
      bef[ch[x][1]] = x;
      bef[fa] = 0;
      pre[ch[x][1]] = 0;
      ch[x][1] = fa;
      pre[fa] = x;
      fa = x;
      push_up(x);
    }
  }
  inline int getRt(int x) {
    Access(x);
    Splay(x);
    push_down(x);
    while (ch[x][0]) {
      x = ch[x][0];
      push_down(x);
    }
    Splay(x);
    return x;
  }
  inline void makeRt(int x) {
    Access(x);
    Splay(x);
    Rev(x);
  }
  inline void Cut(int u, int v) {
    makeRt(u);
    Access(v);
    Splay(v);
    pre[ch[v][0]] = 0;
    ch[v][0] = 0;
    push_up(v);
  }
  inline void Link(int u, int v) {
    makeRt(v);
    push_down(v);
    Access(u);
    Splay(u);
    pre[u] = v;
    ch[v][0] = u;
    push_up(v);
  }
  inline int Query(int x, int y) {
    Access(y);
    int Ansv = 0, Ansf;
    for (y = 0; x; x = bef[x]) {
      Splay(x);
      if (!bef[x]) {
        upd(Ansv, Ansf, mx[y], f[y]);
        upd(Ansv, Ansf, mx[ch[x][1]], f[ch[x][1]]);
        upd(Ansv, Ansf, w[x], x);
        return Ansf;
      }
      sz[x] += sum[ch[x][1]];
      sz[x] -= sum[y];
      bef[ch[x][1]] = x;
      bef[y] = 0;
      pre[ch[x][1]] = 0;
      ch[x][1] = y;
      pre[y] = x;
      y = x;
      push_up(x);
    }
  }
  inline int isOdd(int u, int v) {
    makeRt(u);
    Access(v);
    Splay(v);
    int x = sum[u], y = sum[v] - sum[u];
    return (x & 1) && (y & 1);
  }
} T;
int top, tot, stk[N];
inline int newnode(int w) {
  int t = top ? stk[--top] : ++tot;
  T.bef[t] = T.pre[t] = T.rev[t] = 0;
  T.ch[t][0] = T.ch[t][1] = 0;
  T.f[t] = t;
  T.mx[t] = T.w[t] = w;
  return t;
}
int U[N], V[N], W[N];
set<pair<int, int> > es;
inline void addEdge(int u, int v, int w) {
  if (T.getRt(u) == T.getRt(v)) {
    int Id = T.Query(u, v);
    if (W[Id] <= w) return;
    stk[top++] = Id;
    nOdd += T.isOdd(U[Id], V[Id]) * 2;
    T.Cut(Id, U[Id]);
    T.Cut(Id, V[Id]);
    es.erase(make_pair(W[Id], Id));
  }
  int t = newnode(w);
  U[t] = u;
  V[t] = v;
  W[t] = w;
  es.insert(make_pair(w, t));
  T.Link(t, u);
  T.Link(t, v);
  nOdd -= T.isOdd(u, v) * 2;
}
inline int calAns() {
  while (true) {
    int id = es.rbegin()->second;
    if (T.isOdd(U[id], V[id])) break;
    stk[top++] = id;
    T.Cut(id, U[id]);
    T.Cut(id, V[id]);
    es.erase(make_pair(W[id], id));
  }
  return es.rbegin()->first;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  tot = nOdd = n;
  for (int i = 1; i <= n; ++i) {
    T.sz[i] = T.sum[i] = 1;
  }
  int preAns = 0x3f3f3f3f;
  for (int i = 0; i < m; ++i) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    if (w > preAns) {
      printf("%d\n", preAns);
      continue;
    }
    addEdge(u, v, w);
    if (nOdd == 0) preAns = calAns();
    printf("%d\n", preAns == 0x3f3f3f3f ? -1 : preAns);
  }
  return 0;
}
