#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string.h>
#include <fstream>
#include <cassert>
using namespace std;
#define sz(a) int((a).size())
#define rep(i, s, n) for(int i = s; i <= (n); ++i)
#define rev(i, n, s) for(int i = (n); i >= s; --i)
#define fore(x, a) for(auto &&x : a)
typedef long long ll;
const int mod = 1000000007 ;
const int N = 1005 ;
vector< int > g[ N] ;
int d[ N] [ N] ;
void go( int x, int p, int c, int _depth) {
d[ c] [ _depth] ++ ;
fore( y, g[ x] ) {
if ( y == p) continue ;
go( y, x, c, _depth + 1 ) ;
}
}
class TreeDiameters {
public :
int getMax( vector < int > p ) {
int ans = 0 ;
int n = sz( p) + 1 ;
rep( i, 0 , n - 1 ) {
g[ i] .clear ( ) ;
}
rep( i, 0 , n - 2 ) {
g[ p[ i] ] .push_back ( i + 1 ) ;
g[ i + 1 ] .push_back ( p[ i] ) ;
}
rep( i, 0 , n - 1 ) {
int m = sz( g[ i] ) - 1 ;
rep( j, 0 , m) {
memset ( d[ j] , 0 , sizeof ( d[ j] ) ) ;
}
rep( j, 0 , m) {
go( g[ i] [ j] , i, j, 0 ) ;
}
rep( j, 0 , n - 1 ) {
int tot = 0 ;
rep( k, 0 , m) {
tot + = d[ k] [ j] ;
}
if ( tot == 0 ) break ;
int cur = 0 ;
rep( k, 0 , m) {
cur + = d[ k] [ j] * ( tot - d[ k] [ j] ) ;
}
ans = max( ans, cur/ 2 ) ;
}
}
rep( i, 0 , n - 2 ) {
rep( j, 0 , 1 ) memset ( d[ j] , 0 , sizeof ( d[ j] ) ) ;
go( i + 1 , p[ i] , 0 , 0 ) ;
go( p[ i] , i + 1 , 1 , 0 ) ;
rep( j, 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R5cGVkZWYgbG9uZyBsb25nIGxsOwpjb25zdCBpbnQgbW9kID0gMTAwMDAwMDAwNzsKY29uc3QgaW50IE4gPSAxMDA1OwoKdmVjdG9yPGludD4gZ1tOXTsKaW50IGRbTl1bTl07Cgp2b2lkIGdvKGludCB4LCBpbnQgcCwgaW50IGMsIGludCBfZGVwdGgpIHsKICBkW2NdW19kZXB0aF0rKzsKICBmb3JlKHksIGdbeF0pIHsKICAgIGlmICh5ID09IHApIGNvbnRpbnVlOwogICAgZ28oeSwgeCwgYywgX2RlcHRoICsgMSk7CiAgfQp9CgpjbGFzcyBUcmVlRGlhbWV0ZXJzIHsKcHVibGljOgogIGludCBnZXRNYXgoIHZlY3RvciA8aW50PiBwICkgewogICAgaW50IGFucyA9IDA7CiAgICBpbnQgbiA9IHN6KHApICsgMTsKICAgIHJlcChpLCAwLCBuIC0gMSkgewogICAgICBnW2ldLmNsZWFyKCk7CiAgICB9CiAgICByZXAoaSwgMCwgbiAtIDIpIHsKICAgICAgZ1twW2ldXS5wdXNoX2JhY2soaSArIDEpOwogICAgICBnW2kgKyAxXS5wdXNoX2JhY2socFtpXSk7CiAgICB9CiAgICByZXAoaSwgMCwgbiAtIDEpIHsKICAgICAgaW50IG0gPSBzeihnW2ldKSAtIDE7CiAgICAgIHJlcChqLCAwLCBtKSB7CiAgICAgICAgbWVtc2V0KGRbal0sIDAsIHNpemVvZihkW2pdKSk7CiAgICAgIH0KICAgICAgcmVwKGosIDAsIG0pIHsKICAgICAgICBnbyhnW2ldW2pdLCBpLCBqLCAwKTsKICAgICAgfQogICAgICByZXAoaiwgMCwgbiAtIDEpIHsKICAgICAgICBpbnQgdG90ID0gMDsKICAgICAgICByZXAoaywgMCwgbSkgewogICAgICAgICAgdG90ICs9IGRba11bal07CiAgICAgICAgfQogICAgICAgIGlmICh0b3QgPT0gMCkgYnJlYWs7CiAgICAgICAgaW50IGN1ciA9IDA7CiAgICAgICAgcmVwKGssIDAsIG0pIHsKICAgICAgICAgIGN1ciArPSBkW2tdW2pdICogKHRvdCAtIGRba11bal0pOwogICAgICAgIH0KICAgICAgICBhbnMgPSBtYXgoYW5zLCBjdXIvMik7CiAgICAgIH0KICAgIH0KICAgIHJlcChpLCAwLCBuIC0gMikgewogICAgICByZXAoaiwgMCwgMSkgbWVtc2V0KGRbal0sIDAsIHNpemVvZihkW2pdKSk7CiAgICAgIGdvKGkgKyAxLCBwW2ldLCAwLCAwKTsKICAgICAgZ28ocFtpXSwgaSArIDEsIDEsIDApOwogICAgICByZXAoaiwgMCwgbiAtIDEpIHsKICAgICAgICBpZiAoZFswXVtqXSA9PSAwICYmIGRbMV1bal0gPT0gMCkgYnJlYWs7CiAgICAgICAgYW5zID0gbWF4KGFucywgZFswXVtqXSAqIGRbMV1bal0pOwogICAgICB9CiAgICB9CiAgICByZXR1cm4gYW5zOwogIH0KfTs=