Descirption
Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations connected with n - 1 routes so that each route connects two stations, and it is possible to reach every station from any other.
The boys decided to have fun and came up with a plan. Namely, in some day in the morning Misha will ride the undergrouna was here” on every station he will pass through (including s and f). After that on the same day at evening Grisha will ride from station t to station f by the shortest path and will count stations with Misha’s text. After that at night the underground workers will wash the texts out, because the underground should be clean.
The boys have already chosen the of them should be station s on that day, another should be station f, and the remaining should be station t. They became interested how they should choose these stations s, f, t so that the number Grisha will count is as large as possible. They asked you for help.
Input
The first line contains two integers n and q (2 ≤ n ≤ 105, 1 ≤ q ≤ 105) — the number of stations and the number of days.
The second line contains n - 1 integers p 2, p 3, …, p n (1 ≤ p i ≤ n). The integer p i means that there is a route between stations p i and i.
The next q lines contains three integers a, b and c each (1 ≤ a, b, c ≤ n) . Note that some of these ids could be same.
Output
Print q lines. In the i-th of these lines print the maximum possible number Grisha can get counting when the stations s, t and f are chosen optimally from the three stations on the i-th day.
Examples
Input
3 2
1 1
1 2 3
2 3 3
Output
2
3
Input
4 1
1 2 3
1 2 3
Output
2
Note
In the first example on the first day if s = 1, f = 2, t = 3, Misha would go on the route 1 2, and Grisha would go on the route 3 1 2. He would see the text at the stations 1 and 2. On the second day, if s = 3, f = 2, t = 3, both boys would go on the route 3 1 2. Grisha would see the text at 3 stations.
In the second examle if s = 1, f = 3, t = 2, Misha would go on the route 1 2 3, and Grisha would go on the route 2 3 and would see the text at both stations.
#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e5+5;
int n,m,s,num=0,head[MAXN*2],dep[MAXN*2],f[MAXN*2][23];
struct edg{
int next,to;
}edge[MAXN*2];
void edge_add(int u,int v)//链式前向星存图
{
num++;
edge[++num].next=head[v];edge[num].to=u;head[v]=num;
}
void dfs(int u,int father)//对应深搜预处理f数组
{
dep[u]=dep[father]+1;
for(int i=1;(1<<i)<=dep[u];i++)
{
f[u][i]=f[f[u][i-1]][i-1];
}
for(int i=head[u];i;i=edge[i].next)
{
int v=edge[i].to;
f[v][0]=u;
dfs(v,u);
}
}
int lca(int x,int y)
{
if(dep[x]<dep[y])swap(x,y);
for(int i=20;i>=0;i--)//从大到小枚举使x和y到了同一层
{
if(dep[f[x][i]]>=dep[y])x=f[x][i];
}
for(int i=20;i>=0;i--)//从大到小枚举
{
if(f[x][i]!=f[y][i])//尽可能接近
{
x=f[x][i];y=f[y][i];
}
}
return f[x][0];//随便找一个**输出
}
int main(){
ios::sync_with_stdio(0);
int n,m;
cin>>n>>m;
for(int i=2;i<=n;i++){
int u;
cin>>u;
edge_add(u,i);
}
dep[0]=-1;
dfs(1,0);
int dis[3];
int a,b,c;
for(int i=0;i<m;i++){
cin>>a>>b>>c;
dis[0]=dep[a]+dep[b]-2*dep[lca(a,b)];
dis[1]=dep[a]+dep[c]-2*dep[lca(a,c)];
dis[2]=dep[b]+dep[c]-2*dep[lca(b,c)];
sort(dis,dis+3);
cout<<(dis[2]+dis[1]-dis[0])/2+1<<"\n";
}
return 0;
}