#include <bits/stdc++.h>
using namespace std;
const long long SIZE = 1e5 * 3 + 10, INF = 1e9 * 1000;
vector<vector<long long>> gr;
bool used[SIZE];
long long col[SIZE], c[2];
void dfs(int v) {
  used[v] = 1;
  c[col[v]]++;
  for (auto to : gr[v]) {
    if (!used[to]) {
      col[to] = !col[v];
      dfs(to);
    }
  }
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  ;
  long long n;
  cin >> n;
  gr.resize(n);
  for (int i = 0; i < n - 1; i++) {
    long long u, v;
    cin >> u >> v;
    u--;
    v--;
    gr[u].push_back(v);
    gr[v].push_back(u);
  }
  dfs(0);
  vector<long long> perm(n);
  vector<vector<long long>> cnt(3);
  for (int i = 1; i <= n; i++) {
    cnt[(i % 3)].push_back(i);
  }
  cerr << col[0] << "\n";
  if (c[0] > c[1]) {
    cerr << "baka\n";
    if (c[0] > max(cnt[1].size(), cnt[2].size()) + cnt[0].size()) {
      for (int i = 0; i < n; i++) {
        if (col[i] == 1) {
          long long v = cnt[0].back();
          perm[i] = v;
          cnt[0].pop_back();
        }
      }
      for (int i = 0; i < n; i++) {
        if (perm[i] == 0) {
          for (int j = 0; j < 3; j++) {
            if (cnt[j].size() > 0) {
              long long v = cnt[j].back();
              cnt[j].pop_back();
              perm[i] = v;
              j = 10;
            }
          }
        }
      }
      for (auto cur : perm) cout << cur << " ";
      return 0;
    }
  } else {
    if (c[1] > max(cnt[1].size(), cnt[2].size()) + cnt[0].size()) {
      for (int i = 0; i < n; i++) {
        if (col[i] == 0) {
          long long v = cnt[0].back();
          perm[i] = v;
          cnt[0].pop_back();
        }
      }
      for (int i = 0; i < n; i++) {
        if (perm[i] == 0) {
          for (int j = 0; j < 3; j++) {
            if (cnt[j].size() > 0) {
              long long v = cnt[j].back();
              cnt[j].pop_back();
              perm[i] = v;
              j = 10;
            }
          }
        }
      }
      for (auto cur : perm) cout << cur << " ";
      return 0;
    }
  }
  long long mxPar = 0, mxInd = 1;
  if (c[1] > c[0]) mxPar = 1;
  if (cnt[2].size() > cnt[1].size()) mxInd = 2;
  for (int i = 0; i < n; i++) {
    if (col[i] == mxPar) {
      if (cnt[mxInd].size() > 0) {
        perm[i] = cnt[mxInd].back();
        cnt[mxInd].pop_back();
      }
    } else {
      long long ind = (!(mxInd - 1)) + 1;
      if (cnt[ind].size() > 0) {
        perm[i] = cnt[ind].back();
        cnt[ind].pop_back();
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (perm[i] == 0) {
      if (cnt[0].size() == 0) {
        cout << "-1";
        return 0;
      }
      long long v = cnt[0].back();
      cnt[0].pop_back();
      perm[i] = v;
    }
  }
  for (auto cur : perm) cout << cur << " ";
  return 0;
}
