#include <bits/stdc++.h>
template <typename T>
bool Chkmax(T &x, T y) {
  return x < y ? x = y, true : false;
}
template <typename T>
bool Chkmin(T &x, T y) {
  return y < x ? x = y, true : false;
}
const int MAXN = 1e5 + 1e4, MAXM = 3e5 + 3e4, oo = 0x3f3f3f3f;
int n, m;
int ans[MAXM];
struct Edge {
  int u, v, w, id;
  Edge() {}
  Edge(int u, int v, int w, int id) : u(u), v(v), w(w), id(id) {}
  inline int Diff(int x) {
    if (u == x)
      return v;
    else if (v == x)
      return u;
    else
      return -1;
  }
  inline bool operator<(const Edge &e) const { return w < e.w; }
} G[MAXM];
struct Status {
  int *pos, val;
  Status() {}
  Status(int *pos, int val) : pos(pos), val(val) {}
  Status(int *pos) : pos(pos), val(*pos) {}
  inline void Exec() { (*pos) = val; }
} st[MAXM * 5];
int sumodd;
int top;
int fa[MAXN];
int rank[MAXN];
int size[MAXN];
int Find(int x) { return x == fa[x] ? x : Find(fa[x]); }
void Union(int u, int v) {
  u = Find(u), v = Find(v);
  if (u == v) return;
  if (rank[u] < rank[v]) std::swap(u, v);
  if (rank[u] == rank[v]) {
    st[++top] = Status(&rank[u]);
    ++rank[u];
  }
  st[++top] = Status(&fa[v]);
  st[++top] = Status(&size[u]);
  if ((size[u] & 1) && (size[v] & 1)) {
    st[++top] = Status(&sumodd);
    sumodd -= 2;
  }
  fa[v] = u;
  size[u] += size[v];
}
void Exec(int L, int R, int minans, int maxans, std::vector<Edge> &E) {
  if (L > R) return;
  std::vector<Edge> next;
  int M = (L + R) / 2, fr = top;
  ans[M] = maxans;
  for (int i = 0; i < E.size(); ++i) {
    Edge &e = E[i];
    if (e.id < L && e.w < minans)
      Union(e.u, e.v);
    else if (e.id <= R && e.w < maxans)
      next.push_back(e);
  }
  int back = top;
  for (int i = 0; i < E.size(); ++i) {
    Edge &e = E[i];
    if (e.id <= M) {
      Union(e.u, e.v);
      if (sumodd == 0) {
        ans[M] = e.w;
        break;
      }
    }
  }
  for (int i = top, _ = back + 1; i >= _; --i) st[i].Exec();
  top = back;
  Exec(L, M - 1, ans[M], maxans, next);
  Exec(M + 1, R, minans, ans[M], next);
  for (int i = top, _ = fr + 1; i >= _; --i) st[i].Exec();
  top = fr;
}
std::vector<Edge> edges;
void Solve() {
  sumodd = n;
  for (int i = 1, _ = n; i <= _; ++i) {
    fa[i] = i;
    size[i] = 1;
  }
  std::sort(edges.begin(), edges.end());
  Exec(1, m, 0, oo, edges);
  for (int i = 1, _ = m; i <= _; ++i) {
    printf("%d\n", ans[i] == oo ? -1 : ans[i]);
  }
}
void Input() {
  int u, v, w;
  scanf("%d%d", &n, &m);
  for (int i = 1, _ = m; i <= _; ++i) {
    scanf("%d%d%d", &u, &v, &w);
    edges.push_back(Edge(u, v, w, i));
  }
}
int main() {
  if (fopen("E.in", "r") != NULL) {
    freopen("E.in", "r", stdin);
    freopen("E.out", "w", stdout);
  }
  Input();
  Solve();
  return 0;
}
