/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include<bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

using ll = long long;
using P = pair<ll, ll>;

constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

class CThREE {
public:

    vector<int> graph[200005];
    vector<int> color;

    void dfs(int p, int c) {
      for(auto e: graph[p]) {
        if (color[e] == 0) {
          color[e] = -c;
          dfs(e, -c);
        }
      }
    }

    void solve(istream& cin, ostream& cout) {
      SPEED;
      int n; cin >> n;
      if (n == 2) {
        cout << "1 2" << endl;
        return;
      }

      color.resize(n);
      rep(i, n-1) {
        int a, b; cin >> a >> b;
        a--; b--;
        graph[a].push_back(b);
        graph[b].push_back(a);
      }
      dfs(0, 1);
      int bc = 0;
      int wc = 0;
      int m1 = (n+2)/3;
      int m2 = (n+1)/3;
      int m3 = n/3;

      rep(i, n) {
        if (color[i] == 1) {
          bc++;
        } else {
          wc++;
        }
      }
      debug(bc, wc);

      vector<int> ans(n);
      vector<bool> used(n);

      if (bc <= m3) {
        int p = 1;
        rep(i, n) {
          if (color[i] == 1) {
            debug(i);
            ans[i] = 3*p;
            used[3*p] = true;
            p++;
          }
        }
        int q = 1;
        rep(i, n) {
          if (ans[i] == 0) {
            while(used[q]) {
              q++;
            }
            ans[i] = q;
            used[q] = true;
            q++;
          }
        }
      } else if (wc <= m3) {
        int p = 1;
        rep(i, n) {
          if (color[i] != 1) {
            ans[i] = 3*p;
            used[3*p] = true;
            p++;
          }
        }
        int q = 1;
        rep(i, n) {
          if (ans[i] == 0) {
            while(used[q]) {
              q++;
            }
            ans[i] = q;
            used[q] = true;
            q++;
          }
        }
      } else {
        int p = 1;
        int q = 1;
        int r = 1;
        rep(i, n) {
          if (color[i] == 1 && p <= m1) {
            ans[i] = 3 * p - 2;
            used[3 * p - 2] = true;
            p++;
          } else if (color[i] != 1 && q <= m2) {
            ans[i] = 3 * q - 1;
            used[3 * q - 1] = true;
            q++;
          }
        }
        rep(i, n) {
          if (ans[i] == 0) {
            ans[i] = 3*r;
            r++;
          }
        }
      }

      for(auto e: ans) {
        cout << e << ' ';
      }
      cout << endl;



    }
};

signed main() {
  CThREE solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}

Measured memory_bytes: 15104