//这题让局部有序，两个局部确实无序的，就可以了

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int n,k;
int dat[100000+10];
void merge(int l,int r)
{
    if(k==0)
        return;
    if(r==l)return;
    k-=2;
    vector<int> tmp;
    int mid = (r+l)>>1;
    for (int i = (r-l+1)/2+1; i < r-l+1; i++)
    {
        tmp.push_back(dat[l+i]);
    }
    for (int i = 0;i<=(r-l+1)/2;i++)
    {
        tmp.push_back(dat[l+i]);
    }
    for (int i = 0; i < tmp.size(); i++)
    {
        dat[i+l] = tmp[i];
    }
    merge(l,mid);
    merge(mid+1,r);
}

int main()
{
    //freopen("C:\\Users\\37\\Documents\\project\\atom\\Algorithm\\Algorithm\\output.txt","w",stdout);//
    //freopen("C:\\Users\\37\\Documents\\project\\atom\\Algorithm\\Algorithm\\input.txt","r",stdin);//

    ios::sync_with_stdio(false);

    cin>>n>>k;
    for (int i = 0; i < 100000+10; i++)
    {
        dat[i] = i+1;
    }
    k--;
    if(k&1)
    {
        return cout<<-1<<endl,0;
    }
    merge(0,n-1);
    if(k==0)
    {
        for (int i = 0; i < n; i++)
        {
            cout<<dat[i]<<" ";
        }
        cout<<endl;
    }else{
        cout<<-1<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;

vector<int> vec;

int merge(int l,int r,int remain)
{
    if(remain==0)
    {
        return 0;
    }
    int mid = (l+r)/2;
    for(int i=l;i<mid;i++)
    {
        swap(vec[i],vec[mid+(i-l)]);
    }
    merge(l,mid,remain-2);
    merge(mid,r,remain-2);
    return 0;
}

int main()
{
    ios::sync_with_stdio(false);
    int n,k;
    cin>>n>>k;
    if((k&1)==0)
    {
        cout<<-1<<endl;
        return 0;
    }

    for(int i=1;i<=n;i++)
    {
        vec.push_back(i);
    }
    merge(0,n,k-1);
    for(auto a:vec)
    {
        cout<<a<<" ";
    }
    cout<<endl;

    return 0;
}


#include <iostream>

using namespace std;

int a[100001];

void foo(int l, int r, int &k){
    if(k == 0){
        for(int j = l; j < r; j++)
            a[j] = j + 1;
        return;
    }
    k -= 2;
    int mid = (l+r)/2;
    foo(l, mid, k);
    foo(mid, r, k);
    a[mid - 1] = mid + 1;
    a[mid] = mid;
    return;
}

int main(){
    int n, k;
    cin >> n >> k;
    if(k % 2 == 0){
        cout << "-1\n";
        return 0;
    }
    k--;
    foo(0, n, k);
    if(k){
        cout << "-1\n";
        return 0;
    }
    for(int j = 0; j < n; j++)
        cout << a[j] << " ";
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int a[100001], seg[400001];

void build(int l, int r, int i){
    if(l+1 == r){
        seg[i] = 0;
        return;
    }
    int m = (l+r) >> 1;
    build(l, m, i*2+1);
    build(m, r, i*2+2);
    seg[i] = seg[i*2+1] + seg[i*2+2] + 2;
}

void solve(int l, int r, int i, int k){
    if(l+1 == r) return;
    int m = (l+r) >> 1;
    if(k == 0) return;

    int i1 = l, i2 = m;
    while(i1 < m)
        swap(a[i1], a[i2]), i1++, i2++;

    if(k < 2){
        cout << -1 << endl;
        exit(0);
    }
    k -= 2;
    if(k > seg[i*2+1]){
        solve(l, m, i*2+1, seg[i*2+1]);
        solve(m, r, i*2+1, k - seg[i*2+1]);
    }else{
        solve(l, m, i*2+1, k);
    }
}

int main(){
    ios_base :: sync_with_stdio(0);
    cin.tie(0);

    int n, k;
    cin >> n >> k;
    build(0, n, 0);
    k--;
    if(k < 0 || k > seg[0]) cout << -1 << endl;
    else{
        for(int j = 0; j < n; j++)
            a[j] = j+1;

        solve(0, n, 0, k);
        for(int j = 0; j < n; j++)
            cout << a[j] << " ";
        cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int a[100001], seg[400001];

void build(int l, int r, int i){
    if(l+1 == r){
        seg[i] = 0;
        return;
    }
    int m = (l+r) >> 1;
    build(l, m, i*2+1);
    build(m, r, i*2+2);
    seg[i] = seg[i*2+1] + seg[i*2+2] + 2;
}

void solve(int l, int r, int i, int k){
    if(l+1 == r) return;
    int m = (l+r) >> 1;
    if(k == 0) return;

    int i1 = l, i2 = m;
    while(i1 < m)
        swap(a[i1], a[i2]), i1++, i2++;

    if(k < 2 || k > seg[i]){
        cout << -1 << endl;
        exit(0);
    }
    k -= 2;
    if(k > seg[i*2+1]){
        solve(l, m, i*2+1, seg[i*2+1]);
        solve(m, r, i*2+1, k - seg[i*2+1]);
    }else{
        solve(l, m, i*2+1, k);
    }
}

int main(){
    ios_base :: sync_with_stdio(0);
    cin.tie(0);

    int n, k;
    cin >> n >> k;
    build(0, n, 0);
    k--;
    if(k < 0 || k > seg[0]) cout << -1 << endl;
    else{
        for(int j = 0; j < n; j++)
            a[j] = j+1;

        solve(0, n, 0, k);
        for(int j = 0; j < n; j++)
            cout << a[j] << " ";
        cout << endl;
    }
    return 0;
}


#include <iostream>

using namespace std;

int a[100001];

void foo(int l, int r, int &k){
    if(k == 0){
        for(int j = l; j < r; j++)
            a[j] = j + 1;
        return;
    }
    k -= 2;
    int mid = (l+r)/2;
    foo(l, mid, k);
    foo(mid, r, k);
    swap(a[mid], a[mid - 1]);
    return;
}

int main(){
    int n, k;
    cin >> n >> k;
    if(k % 2 == 0){
        cout << "-1\n";
        return 0;
    }
    k--;
    foo(0, n, k);
    if(k){
        cout << "-1\n";
        return 0;
    }
    for(int j = 0; j < n; j++)
        cout << a[j] << " ";
    cout << endl;
    return 0;
}


#include <iostream>

using namespace std;

int a[100001];

void foo(int l, int r, int &k){
    if(l == r - 1){
        a[l] = l+1;
        return;
    }
    if(k == 0){
        for(int j = l; j < r; j++)
            a[j] = j + 1;
        return;
    }
    k -= 2;
    int mid = (l+r)/2, temp = k;
    foo(l, mid, k);
    foo(mid, r, k);
    if(k == temp) swap(a[mid], a[mid - 1]);
    return;
}

int main(){
    int n, k;
    cin >> n >> k;
    if(k % 2 == 0){
        cout << "-1\n";
        return 0;
    }
    k--;
    foo(0, n, k);
    if(k){
        cout << "-1\n";
        return 0;
    }
    for(int j = 0; j < n; j++)
        cout << a[j] << " ";
    cout << endl;
    return 0;
}


#include <iostream>

using namespace std;

int a[100001];

void foo(int l, int r, int &k){
    if(k == 0){
        for(int j = l; j < r; j++)
            a[j] = j + 1;
        return;
    }
    k -= 2;
    int mid = (l+r)/2;
    foo(l, mid, k);
    foo(mid, r, k);
    swap(a[mid], a[mid - 1]);
    return;
}

int main(){
    int n, k;
    cin >> n >> k;
    if(k % 2 == 0){
        cout << "-1\n";
        return 0;
    }
    k--;
    foo(0, n, k);
    if(k){
        cout << "-1\n";
        return 0;
    }
    for(int j = 0; j < n; j++)
        if(a[j] == 0) a[j] = 1;

    for(int j = 0; j < n; j++)
        cout << a[j] << " ";
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int a[100001], seg[400001];

void build(int l, int r, int i){
    if(l+1 == r){
        seg[i] = 0;
        return;
    }
    int m = (l+r) >> 1;
    build(l, m, i*2+1);
    build(m, r, i*2+2);
    seg[i] = seg[i*2+1] + seg[i*2+2] + 2;
}

void solve(int l, int r, int i, int k){
    //cout << l << " " << r << " " << k << " " << seg[i] << endl;
    if(l+1 == r) return;
    int m = (l+r) >> 1;
    if(k == 0) return;

    int i1 = l, i2 = m;
    while(i1 < m)
        swap(a[i1], a[i2]), i1++, i2++;

    if(k < 2 || k > seg[i]){
        cout << -1 << endl;
        exit(0);
    }
    k -= 2;
    if(k > seg[i*2+1]){
        solve(l, m, i*2+1, seg[i*2+1]);
        solve(m, r, i*2+2, k - seg[i*2+1]);
    }else{
        solve(l, m, i*2+1, k);
    }
}

int main(){
    ios_base :: sync_with_stdio(0);
    cin.tie(0);

    int n, k;
    cin >> n >> k;
    build(0, n, 0);
    k--;
    if(k < 0 || k > seg[0]) cout << -1 << endl;
    else{
        for(int j = 0; j < n; j++)
            a[j] = j+1;

        solve(0, n, 0, k);
        for(int j = 0; j < n; j++)
            cout << a[j] << " ";
        cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int c=0;
void modify_array(vector<ll>& v,ll l,ll r,ll k){
    //cout<<l<<" "<<r<<endl;
    if(c>=k || l==r-1) return;
    ll m=(l+r)/2;
    c+=2;
    swap(v[m],v[m-1]);
    modify_array(v,l,m,k);
    modify_array(v,m,r,k);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    ll n,k;
    cin>>n>>k;
    vector<ll> v(n);
    for(ll i=0;i<n;i++){
        v[i]=i+1;
    }
    c++;
    modify_array(v,0,n,k-1);
    if(c>k || k>(2*n-1)){
        cout<<-1<<endl;
    }
    else{
//    	cout<<c;
        for(ll i:v)cout<<i<<" ";
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int c=1;
void modify_array(vector<int>& v,int l,int r,int k){
    if(c>=k) return;
    int m=(l+r)/2;
    c+=2;
    swap(v[m],v[m+1]);
    modify_array(v,l,m,k);
    modify_array(v,m+1,r,k);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int n,k;
    cin>>n>>k;
    vector<int> v(n);
    for(int i=0;i<n;i++){
        v[i]=i+1;
    }
    modify_array(v,0,n-1,k);
    if(c>k){
        cout<<-1<<endl;
    }
    else{
        for(int i:v)cout<<i<<" ";
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int c=0;
void modify_array(vector<int>& v,int l,int r,int k){
    //cout<<l<<" "<<r<<endl;
    if(c>=k || l==r) return;
    int m=(l+r)/2;
    c+=2;
    swap(v[m],v[m+1]);
    modify_array(v,l,m,k);
    modify_array(v,m+1,r,k);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int n,k;
    cin>>n>>k;
    vector<int> v(n);
    for(int i=0;i<n;i++){
        v[i]=i+1;
    }
    modify_array(v,0,n-1,k-1);
    if(c>k || k>(2*n-1)){
        cout<<-1<<endl;
    }
    else{
    	cout<<c<<endl;
        for(int i:v)cout<<i<<" ";
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int c=1;
void modify_array(vector<int>& v,int l,int r,int k){
    cout<<l<<" "<<r<<endl;
    if(c>=k || l==r) return;
    int m=(l+r)/2;
    c+=2;
    swap(v[m],v[m+1]);
    modify_array(v,l,m,k);
    modify_array(v,m+1,r,k);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int n,k;
    cin>>n>>k;
    vector<int> v(n);
    for(int i=0;i<n;i++){
        v[i]=i+1;
    }
    modify_array(v,0,n-1,k-1);
    if(c>k){
        cout<<-1<<endl;
    }
    else{
        for(int i:v)cout<<i<<" ";
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int c=1;
void modify_array(vector<int>& v,int l,int r,int k){
    //cout<<l<<" "<<r<<endl;
    if(c>=k || l==r) return;
    int m=(l+r)/2;
    c+=2;
    swap(v[m],v[m+1]);
    modify_array(v,l,m,k);
    modify_array(v,m+1,r,k);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int n,k;
    cin>>n>>k;
    vector<int> v(n);
    for(int i=0;i<n;i++){
        v[i]=i+1;
    }
    modify_array(v,0,n-1,k-1);
    if(c>k){
        cout<<-1<<endl;
    }
    else{
        for(int i:v)cout<<i<<" ";
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int c=1;
void modify_array(vector<int>& v,int l,int r,int k){
    if(c>=k || l==r) return;
    int m=(l+r)/2;
    c+=2;
    swap(v[m],v[m+1]);
    modify_array(v,l,m,k);
    modify_array(v,m+1,r,k);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int n,k;
    cin>>n>>k;
    vector<int> v(n);
    for(int i=0;i<n;i++){
        v[i]=i+1;
    }
    modify_array(v,0,n-1,k);
    if(c>k){
        cout<<-1<<endl;
    }
    else{
        for(int i:v)cout<<i<<" ";
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int c=0;
void modify_array(vector<ll>& v,ll l,ll r,ll k){
    //cout<<l<<" "<<r<<endl;
    if(c>=k || l==r-1) return;
    ll m=(l+r)/2;
    c+=2;
    swap(v[m],v[m-1]);
    modify_array(v,l,m,k);
    modify_array(v,m,r,k);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    ll n,k;
    cin>>n>>k;
    vector<ll> v(n);
    for(ll i=0;i<n;i++){
        v[i]=i+1;
    }
    c++;
    modify_array(v,0,n,k-1);
    if(k%2==0 || k>(2*n-1)){
        cout<<-1<<endl;
    }
    else{
//    	cout<<c;
        for(ll i:v)cout<<i<<" ";
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int c=0;
void swap(int& a,int& b){
    int temp=a;
    a=b;
    b=temp;
}
void modify_array(vector<int>& v,int l,int r,int k){
    //cout<<l<<" "<<r<<endl;
    if(c>=k || l==r) return;
    int m=(l+r)/2;
    c+=2;
    swap(v[m],v[m+1]);
    modify_array(v,l,m,k);
    modify_array(v,m+1,r,k);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int n,k;
    cin>>n>>k;
    vector<int> v(n);
    for(int i=0;i<n;i++){
        v[i]=i+1;
    }
    modify_array(v,0,n-1,k-1);
    if(c>k){
        cout<<-1<<endl;
    }
    else{
        for(int i:v)cout<<i<<" ";
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
void unsort(int a[],int min,int max,int count,int k)
{
	int mid= (min+max)/2;
	count++;
	if(min==max || count==k)
		return;
	swap(a[mid],a[mid+1]);
	unsort(a,min,mid,count,k);
	unsort(a,mid+1,max,count,k);
}
int main()
{
    cin.tie(0);cout.tie(0);
	int a[100002];
	int n,i,k;
	cin>>n>>k;
	for(i=0;i<n;i++)
		a[i]=i+1;
	if(k%2==0)
	{
		cout<<-1<<endl;
		return 0;
	}
	int x = ceil(log2(k));
	if(k + 1 == pow(2, x))
	{
		unsort(a,0,n-1,0,k);
	}
	for(i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int cnt;
int k;
void mergesort(int a[],int start,int end)
{
	if(cnt==k)
		return;
	if(end-start<=1)
		return;
	int mid=(start+end)/2;
	swap(a[mid],a[mid-1]);
	cnt+=2;
	if(cnt==k)
		return;
	mergesort(a,start,mid);
	mergesort(a,mid,end);
}
int main()
{
	cin.tie(0);cout.tie(0);
	int n,a[100002];
	cin>>n>>k;
	for(int i=0;i<n;i++)
	{
		a[i]=i+1;
	}
	if(k%2==0)
	{
		cout<<-1<<endl;
		return 0;
	}
	cnt=1;
	mergesort(a,0,n);
	for(int i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
void unsort(int a[],int min,int max,int count,int k)
{
	int mid= (min+max)/2;
	if(min==max || count==k)
		return;
	swap(a[mid],a[mid+1]);
	count+=2;
	unsort(a,min,mid,count,k);
	if(k-count>2)
		unsort(a,mid+1,max,count,k);
}
int main()
{
    cin.tie(0);cout.tie(0);
	int a[100002];
	int n,i,k;
	cin>>n>>k;
	for(i=0;i<n;i++)
		a[i]=i+1;
	if(k%2==0)
	{
		cout<<-1<<endl;
		return 0;
	}
	int x = ceil(log2(k));
	if(k!=1)
		unsort(a,0,n-1,0,k);
	for(i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int cnt;
int k;
void mergesort(int a[],int start,int end)
{
	if(cnt==k)
		return;
	if(end-start<=1)
		return;
	int mid=(start+end)/2;
	swap(a[mid],a[mid-1]);
	cnt+=2;
	if(cnt==k)
		return;
	mergesort(a,start,mid);
	mergesort(a,mid,end);
}
int main()
{
	cin.tie(0);cout.tie(0);
	int n,a[100002];
	cin>>n>>k;
	for(int i=0;i<n;i++)
	{
		a[i]=i+1;
	}
	if(k%2==0 || k>2*n)
	{
		cout<<-1<<endl;
		return 0;
	}
	cnt=1;
	mergesort(a,0,n);
	for(int i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
void unsort(int a[],int min,int max,int count,int k)
{
	int mid= (min+max)/2;
	count++;
	if(min==max || count==k)
		return;
	swap(a[mid],a[mid+1]);
	unsort(a,min,mid,count,k);
	unsort(a,mid+1,max,count,k);
}
int main()
{
    cin.tie(0);cout.tie(0);
	int a[100002];
	int n,i,k;
	cin>>n>>k;
	for(i=0;i<n;i++)
		a[i]=i+1;
	if(k%2==0)
	{
		cout<<-1<<endl;
		return 0;
	}
	int x = ceil(log2(k));
	if(k!=1)
		unsort(a,0,n-1,1,k);
	for(i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
void unsort(int a[],int min,int max,int count,int k)
{
	int mid= (min+max)/2;
	count++;
	if(min==max || count==k)
		return;
	swap(a[mid],a[mid+1]);
	unsort(a,min,mid,count,k);
	unsort(a,mid+1,max,count,k);
}
int main()
{
    cin.tie(0);cout.tie(0);
	int a[100002];
	int n,i,k;
	cin>>n>>k;
	for(i=0;i<n;i++)
		a[i]=i+1;
	if(k%2==0)
	{
		cout<<-1<<endl;
		return 0;
	}
	int x = ceil(log2(k));
	if(k!=1)
		unsort(a,0,n-1,0,k);
	for(i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
void unsort(int a[],int min,int max,int count,int k)
{
	int mid= (min+max)/2;
	count++;
	if(min==max || count==k)
		return;
	swap(a[mid],a[mid+1]);
	unsort(a,min,mid,count,k);
	if(k-count>2)
		unsort(a,mid+1,max,count,k);
}
int main()
{
    cin.tie(0);cout.tie(0);
	int a[100002];
	int n,i,k;
	cin>>n>>k;
	for(i=0;i<n;i++)
		a[i]=i+1;
	if(k%2==0)
	{
		cout<<-1<<endl;
		return 0;
	}
	int x = ceil(log2(k));
	if(k!=1)
		unsort(a,0,n-1,0,k);
	for(i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
void mergesort(int a[],int start,int end,int k,int count)
{
	if(count==k)
		return ;
	if(start>=end)
		return ;
	int mid=(start+end)/2;
	swap(a[mid],a[mid+1]);
	count+=2;
	mergesort(a,start,mid,k,count);
    mergesort(a,mid+1,end,k,count);
}
int main()
{
	cin.tie(0);cout.tie(0);
	int n,k,a[100002];
	cin>>n>>k;
	for(int i=0;i<n;i++)
	{
		a[i]=i+1;
    }
    if(k%2==0)
    {
        cout<<-1<<endl;
        return 0;
    }
	mergesort(a,0,n-1,k,1);
    for(int i=0;i<n;i++)
        cout<<a[i]<<" ";
    cout<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m;
int a[N],b[N];
void dfs(int l,int r)
{
	int i,j,mid=l+r+1>>1;
	if(m)
	{
		m-=2;
		for(i=l;i<=r;i++) b[i]=a[i];
		for(i=mid-1,j=r;i>=l;i--,j--) a[i]=b[j];
		for(i=mid,j=l;i<=r;i++,j++) a[i]=b[j];
		dfs(l,mid-1),dfs(mid,r);
	}
}
int main()
{
	int i;
	scanf("%d%d",&n,&m);
	if(m%2==0||m>n*2-1) return puts("-1"),0;
	for(i=1;i<=n;i++) a[i]=i;
	m--,dfs(1,n);
	for(i=1;i<=n;i++) printf("%d ",a[i]);puts("");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m;
int a[N],b[N];
void dfs(int l,int r)
{
	int i,mid=l+r+1>>1;
	if(m)
	{
		m-=2;
		for(i=l;i<=r;i++) b[i]=a[i];
		for(i=mid-1;i;i--) a[i]=a[i-mid+1+r];
		for(i=mid;i<=r;i++) a[i]=b[i-mid+l];
		dfs(l,mid-1),dfs(mid,r);
	}
}
int main()
{
	int i;
	scanf("%d%d",&n,&m);
	if(m%2==0||m>n*2-1) return puts("-1"),0;
	for(i=1;i<=n;i++) a[i]=i;
	m--,dfs(1,n);
	for(i=1;i<=n;i++) printf("%d ",a[i]);puts("");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,m;
int a[N];
void dfs(int l,int r)
{
	int i,mid=l+r+1>>1;
	if(m)
	{
		m-=2;
		dfs(l,mid-1),dfs(mid,r);
		int i,x=l,y=mid;
		for(i=l;i<mid;i++) if(a[i]>a[x]) x=i;
		for(i=mid;i<=r;i++) if(a[i]<a[y]) y=i;
		swap(a[x],a[y]);
	}
}
int main()
{
	int i;
	scanf("%d%d",&n,&m);
	if(m%2==0||m>n*2-1) return puts("-1"),0;
	for(i=1;i<=n;i++) a[i]=i;
	m--,dfs(1,n);
	for(i=1;i<=n;i++) printf("%d ",a[i]);puts("");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m;
int a[N],b[N];
void dfs(int l,int r)
{
	int i,j,mid=l+r+1>>1;
	if(m&&l<r)
	{
		m-=2;
		for(i=l;i<=r;i++) b[i]=a[i];
		for(i=mid-1,j=r;i>=l;i--,j--) a[i]=b[j];
		for(i=mid,j=l;i<=r;i++,j++) a[i]=b[j];
		dfs(l,mid-1),dfs(mid,r);
	}
}
int main()
{
	int i;
	scanf("%d%d",&n,&m);
	if(m%2==0||m>n*2-1) return puts("-1"),0;
	for(i=1;i<=n;i++) a[i]=i;
	m--,dfs(1,n);
	for(i=1;i<=n;i++) printf("%d ",a[i]);puts("");
	return 0;
}


#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <iterator>
using namespace std;

void swp(vector<int>& v, int l, int r, int c)
{
    if (c <= 1) return;
    int mid = (l + r) / 2;
    swap(v[mid - 1], v[mid]);
    c--;
    int cl = min(c / 2, (mid - l) * 2 -1);
    if (cl == 2) {
        cl = 1;
    }
    swp(v, l, mid, cl);
    swp(v, mid, r, c - cl);
}

int main()
{
    int n, k;
    cin >> n >> k;
    if (k % 2 == 0 || k > 2 * n - 1) {
        cout << -1 << endl;
        return 0;
    }
    vector<int> arr(n);
    iota(arr.begin(), arr.end(), 1);
    swp(arr, 0, n, k);
    copy(arr.begin(), arr.end(), ostream_iterator<int>(cout, " "));
    cout << endl;
}


#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <iterator>
using namespace std;

void swp(vector<int>& v, int l, int r, int c)
{
    if (c <= 1) return;
    int mid = (l + r) / 2;
    swap(v[mid - 1], v[mid]);
    c--;
    swp(v, l, mid, c / 2);
    swp(v, mid, r, c - c / 2);
}

int main()
{
    int n, k;
    cin >> n >> k;
    if (k % 2 == 0 || k > 2 * n - 1) {
        cout << -1 << endl;
        return 0;
    }
    vector<int> arr(n);
    iota(arr.begin(), arr.end(), 1);
    swp(arr, 0, n, k);
    copy(arr.begin(), arr.end(), ostream_iterator<int>(cout, " "));
    cout << endl;
}


#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <iterator>
using namespace std;

void swp(vector<int>& v, int l, int r, int c)
{
    if (c <= 1) return;
    int mid = (l + r) / 2;
    swap(v[mid - 1], v[mid]);
    c--;
    // int cl = min(c / 2, (mid - l) * 2 -1);
    // if (cl == 2) {
    //     cl = 1;
    // }
    int cl = min(c - 1, (mid - l) * 2 - 1);
    swp(v, l, mid, cl);
    swp(v, mid, r, c - cl);
}

int main()
{
    int n, k;
    cin >> n >> k;
    if (k % 2 == 0 || k > 2 * n - 1) {
        cout << -1 << endl;
        return 0;
    }
    vector<int> arr(n);
    iota(arr.begin(), arr.end(), 1);
    swp(arr, 0, n, k);
    copy(arr.begin(), arr.end(), ostream_iterator<int>(cout, " "));
    cout << endl;
}


#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <iterator>
using namespace std;

void swp(vector<int>& v, int l, int r, int c)
{
    if (c <= 1) return;
    int mid = (l + r) / 2;
    swap(v[mid - 1], v[mid]);
    c--;
    swp(v, l, mid, c / 2);
    swp(v, mid, r, c / 2);
}

int main()
{
    int n, k;
    cin >> n >> k;
    if (k % 2 == 0 || k > 2 * n - 1) {
        cout << -1 << endl;
        return 0;
    }
    vector<int> arr(n);
    iota(arr.begin(), arr.end(), 1);
    swp(arr, 0, n, k);
    copy(arr.begin(), arr.end(), ostream_iterator<int>(cout, " "));
    cout << endl;
}


#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <iterator>
using namespace std;

void swp(vector<int>& v, int l, int r, int c)
{
    if (c <= 1) return;
    int mid = (l + r) / 2;
    swap(v[mid - 1], v[mid]);
    c--;
    int cl = min(c / 2, (mid - l) * 2 -1);
    swp(v, l, mid, cl);
    swp(v, mid, r, c - cl);
}

int main()
{
    int n, k;
    cin >> n >> k;
    if (k % 2 == 0 || k > 2 * n - 1) {
        cout << -1 << endl;
        return 0;
    }
    vector<int> arr(n);
    iota(arr.begin(), arr.end(), 1);
    swp(arr, 0, n, k);
    copy(arr.begin(), arr.end(), ostream_iterator<int>(cout, " "));
    cout << endl;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int n, k, a[100001], tmp[100001];
void solve(int l, int r)
{
    if (!k || l + 1 == r)
    {
        return;
    }
    int mid = (l + r) >> 1, t = 0;
    for (int i = l; i < l + r - mid; i++)
    {
        tmp[++t] = a[i];
    }
    for (int i = l; i < mid; i++)
    {
        a[i] = a[i + r - mid];
    }
    for (int i = mid; i < r; i++)
    {
        a[i] = tmp[i - mid + 1];
    }
    k -= 2;
    solve(l, mid);
    solve(mid, r);
}
int main(int argc, char const *argv[])
{
    scanf("%d%d", &n, &k);
    if (!(k % 2))
    {
        printf("-1");
        return 0;
    }
    for (int i = 0; i < n; i++)
    {
        a[i] = i + 1;
    }
    k--;
    solve(0, n);
    for (int i = 0; i < n; i++)
    {
        printf("%d ", a[i]);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int n, k, a[100001], tmp[100001];
void solve(int l, int r)
{
    if (!k)
    {
        return;
    }
    int mid = (l + r) >> 1, t = 0;
    for (int i = l; i < l + r - mid; i++)
    {
        tmp[++t] = a[i];
    }
    for (int i = l; i < mid; i++)
    {
        a[i] = a[i + r - mid + l];
    }
    for (int i = mid; i < r; i++)
    {
        a[i] = tmp[i - mid + 1];
    }
    k -= 2;
    solve(l, mid);
    solve(mid, r);
}
int main(int argc, char const *argv[])
{
    scanf("%d%d", &n, &k);
    if (!(k % 2))
    {
        printf("-1");
        return 0;
    }
    for (int i = 0; i < n; i++)
    {
        a[i] = i + 1;
    }
    k--;
    solve(0, n);
    for (int i = 0; i < n; i++)
    {
        printf("%d ", a[i]);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int n, k, a[100001], tmp[100001];
void solve(int l, int r)
{
    if (!k)
    {
        return;
    }
    int mid = (l + r) >> 1, t = 0;
    for (int i = l; i < mid; i++)
    {
        tmp[++t] = a[i];
    }
    for (int i = l; i < l + r - mid; i++)
    {
        a[i] = a[i + mid - l];
    }
    for (int i = r - mid + l; i < r; i++)
    {
        a[i] = tmp[i - r + mid + 1];
    }
    k -= 2;
    solve(l, mid);
    solve(mid, r);
}
int main(int argc, char const *argv[])
{
    scanf("%d%d", &n, &k);
    if (!(k % 2))
    {
        printf("-1");
        return 0;
    }
    for (int i = 0; i < n; i++)
    {
        a[i] = i + 1;
    }
    k--;
    solve(0, n);
    for (int i = 0; i < n; i++)
    {
        printf("%d ", a[i]);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int n, k, a[100001], tmp[100001];
void solve(int l, int r)
{
    if (!k || l + 1 == r)
    {
        return;
    }
    int mid = (l + r) >> 1, t = 0;
    for (int i = l; i < l + r - mid; i++)
    {
        tmp[++t] = a[i];
    }
    for (int i = l; i < mid; i++)
    {
        a[i] = a[i + r - mid];
    }
    for (int i = mid; i < r; i++)
    {
        a[i] = tmp[i - mid + 1];
    }
    k -= 2;
    solve(l, mid);
    solve(mid, r);
}
int main(int argc, char const *argv[])
{
    scanf("%d%d", &n, &k);
    if (!(k % 2))
    {
        printf("-1");
        return 0;
    }
    for (int i = 0; i < n; i++)
    {
        a[i] = i + 1;
    }
    k--;
    solve(0, n);
    if (k)
    {
        printf("-1");
        return 0;
    }
    for (int i = 0; i < n; i++)
    {
        printf("%d ", a[i]);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int n, k, a[100001], tmp[100001];
void solve(int l, int r)
{
    if (!k)
    {
        return;
    }
    int mid = (l + r) >> 1, t = 0;
    for (int i = l; i <= mid; i++)
    {
        tmp[++t] = a[i];
    }
    for (int i = l; i < l + r - 1 - mid; i++)
    {
        a[i] = a[i + mid - l + 1];
    }
    for (int i = r - mid - 1; i < r; i++)
    {
        a[i] = tmp[i - r + mid + 2];
    }
    k -= 2;
    solve(l, mid);
    solve(mid + 1, r);
}
int main(int argc, char const *argv[])
{
    scanf("%d%d", &n, &k);
    if (!(k % 2))
    {
        printf("-1");
        return 0;
    }
    for (int i = 0; i < n; i++)
    {
        a[i] = i + 1;
    }
    k--;
    solve(0, n);
    for (int i = 0; i < n; i++)
    {
        printf("%d ", a[i]);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,k;
int a[100005];
void By_GK_swap(int &x,int &y)
{
	x+=y;
	y=x-y;
	x=x-y;
}
void dfs(int l,int r)
{
	if (l+1>=r||!k)
		return;
	int mid=(l+r) >> 1;
	--k,k--;
	By_GK_swap(a[mid],a[mid-1]);
	dfs(l,mid);
	dfs(mid,r);
}
void solve()
{
	k--;
	dfs(0,n);
	if (k)
		printf("-1\n"); else
		{
			for (int i=0;i<n;i++)
				printf("%d ",a[i]);
			printf("\n");
		}
}
int main()
{
	scanf("%d%d",&n,&k);
	for (int i=0;i<n;i++)
		a[i]=i+1;
	if (k%2==0)
		printf("-1\n"); else
		solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,k;
int a[100005];
void By_GK_swap(int &x,int &y)
{
	x+=y;
	y=x-y;
	x=x-y;
}
void dfs(int l,int r)
{
	if (!(l+1^r)||!k)
		return;
	int mid=(l+r) >> 1;
	--k,k--;
	By_GK_swap(a[mid],a[mid-1]);
	dfs(l,mid);
	dfs(mid,r);
}
void solve()
{
	k--;
	dfs(0,n-1);
	if (k)
		printf("-1\n"); else
		{
			for (int i=0;i<n;i++)
				printf("%d ",a[i]);
			printf("\n");
		}
}
int main()
{
	scanf("%d%d",&n,&k);
	for (int i=0;i<n;i++)
		a[i]=i+1;
	if (k%2==0)
		printf("-1\n"); else
		solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,k;
int a[100005];
void By_GK_swap(int &x,int &y)
{
	x+=y;
	y=x-y;
	x=x-y;
}
void dfs(int l,int r)
{
	if (!(l^r)||!k)
		return;
	int mid=(l+r) >> 1;
	--k,k--;
	By_GK_swap(a[mid],a[mid-1]);
	dfs(l,mid);
	dfs(mid,r);
}
void solve()
{
	k--;
	dfs(0,n-1);
	if (k)
		printf("-1\n"); else
		{
			for (int i=0;i<n;i++)
				printf("%d ",a[i]);
			printf("\n");
		}
}
int main()
{
	scanf("%d%d",&n,&k);
	for (int i=0;i<n;i++)
		a[i]=i+1;
	if (k%2==0)
		printf("-1\n"); else
		solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,k;
int a[100005];
void By_GK_swap(int &x,int &y)
{
	x+=y;
	y=x-y;
	x=x-y;
}
void dfs(int l,int r)
{
	if (l+1>=r)||!k)
		return;
	int mid=(l+r) >> 1;
	--k,k--;
	By_GK_swap(a[mid],a[mid-1]);
	dfs(l,mid);
	dfs(mid,r);
}
void solve()
{
	k--;
	dfs(0,n);
	if (k)
		printf("-1\n"); else
		{
			for (int i=0;i<n;i++)
				printf("%d ",a[i]);
			printf("\n");
		}
}
int main()
{
	scanf("%d%d",&n,&k);
	for (int i=0;i<n;i++)
		a[i]=i+1;
	if (k%2==0)
		printf("-1\n"); else
		solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,k;
int a[100005];
void By_GK_swap(int &x,int &y)
{
	x+=y;
	y=x-y;
	x=x-y;
}
void dfs(int l,int r)
{
	if (!(l^r)||!k)
		return;
	int mid=(l+r+1) >> 1;
	--k,k--;
	By_GK_swap(a[mid],a[mid-1]);
	dfs(l,mid);
	dfs(mid+1,r);
}
void solve()
{
	k--;
	dfs(1,n);
	if (k)
		printf("-1\n"); else
		{
			for (int i=1;i<=n;i++)
				printf("%d ",a[i]);
			printf("\n");
		}
}
int main()
{
	scanf("%d%d",&n,&k);
	for (int i=1;i<=n;i++)
		a[i]=i;
	if (k%2==0)
		printf("-1\n"); else
		solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,k;
int a[100005];
void dfs(int l,int r)
{
	if (!(l^r)||!k)
		return;
	int mid=(l+r+1) >> 1;
	--k,k--;
	swap(a[mid],a[mid-1]);
	dfs(l,mid);
	dfs(mid+1,r);
}
void solve()
{
	k--;
	dfs(1,n);
	if (k)
		printf("-1\n"); else
		{
			for (int i=1;i<=n;i++)
				printf("%d ",a[i]);
			printf("\n");
		}
}
int main()
{
	scanf("%d%d",&n,&k);
	for (int i=1;i<=n;i++)
		a[i]=i;
	if (k%2==0)
		printf("-1\n"); else
		solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,k;
int a[100005];
void dfs(int l,int r)
{
	if (!(l^r)||!k)
		return;
	int mid=(l+r+1) >> 1;
	--k,k--;
	swap(a[mid],a[mid+1]);
	dfs(l,mid);
	dfs(mid+1,r);
}
void solve()
{
	k--;
	dfs(1,n);
	if (k)
		printf("-1\n"); else
		{
			for (int i=1;i<=n;i++)
				printf("%d ",a[i]);
			printf("\n");
		}
}
int main()
{
	scanf("%d%d",&n,&k);
	for (int i=1;i<=n;i++)
		a[i]=i;
	if (k%2==0)
		printf("-1\n"); else
		solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 100005;

int a[N];
int n, k, p;

void divide(int l, int r) {
    if (k < 0) {
        return;
    }
    if (k == 0) {
        int q = p - (r - l) + 1;
        for (int i = 0; i < (r - l); ++i) {
            a[i + l] = q + i;
        }
        p = q - 1;
        return;
    }
    if (l + 1 == r) {
        // cannot go down further
        a[l] = p--;
        return;
    }
    // try adding nodes from below
    int m = (l + r) >> 1;
    k -= 2;
    divide(l, m);
    divide(m, r);
}

int main() {
    scanf("%d%d", &n, &k);
    ::p = n;
    --k;
    divide(0, n);
    // cout << k << ' ' << p << endl;
    if (k == 0 && p == 0) {
        for (int i = 0; i < n; ++i) {
            if (i) printf(" ");
            printf("%d", a[i]);
        }
    } else {
        printf("-1\n");
    }
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1000000;

int n, k;
int lg[N];
// 3 => 1 => 2 => 0
// 7 => 3 => 5 => 1 => 6 => 2 => 4 => 0
// 111
// 011
// 101
// 001
// 110
// 010
// 100
// 000
int ord[N], dp[N], arr[N], tmp[N];

void gen(int size) {
    int bits = lg[size];
    for (int i = 0; i < size; ++i) {
        // flip bits
        int mask = i;
        int rev = 0;
        while (mask > 0) {
            int big = lg[mask];
            rev |= 1 << (bits - big - 1);
            mask ^= 1 << big;
        }
        ord[size - i - 1] = rev;
    }
}

int mergeSort(int L, int R) {
    int res = 1;
    bool need = false;
    for (int i = L + 1; i < R; ++i) {
        if (!(arr[i - 1] <= arr[i])) {
            need = true;
            break;
        }
    }
    if (need) {
        int M = (L + R) / 2;
        res += mergeSort(L, M) + mergeSort(M, R);
        merge(arr + L, arr + M, arr + M, arr + R, tmp);
        copy(tmp, tmp + (R - L), arr + L);
    } else {
        // static int ptr = 0;
        // cout << "Leaf: " << L << " " << R << " " << R-L << " " << dp[ptr++] << endl;
    }
    return res;
}

int main() {
    for (int i = 2; i < N; ++i) lg[i] = lg[i >> 1] + 1;
    scanf("%d%d", &n, &k);
    // while (true) {
    //     n = rand() % 100000 + 1;
    //     k = rand() % 200000 + 1;
    if (k % 2 == 0) printf("-1\n");
        else {
            int start = 1 << lg[k];
            int end = 2 << lg[k];
            int size = end - start;
            gen(size);
            for (int i = 0; i < size; ++i) dp[i] = 0;
            int cursize = 0;
            if (n >= size) {

                // allocate
                for (int i = 0; i < size; ++i) dp[i] = 1;

                // generate order
                gen(size);

                // add extra elements one by one
                int cursize = size;
                int ptr = 0;
                while (cursize < n) {
                    dp[ord[ptr++]] += 1;
                    ptr %= size;
                    ++cursize;
                }

                // cout << "size: " <<  cursize << endl;

                // construct array
                int p = n;
                int m = 0;

                for (int i = 0; i < size&& start + i <= k; ++i) {
                    int q = p - dp[i] + 1;
                    for (int j = q; j <= p; ++j)
                        arr[m++] = j;
                    p = q - 1;
                }
                // cout << "after: " << p << ' ' << end - k << ' ' << endl;

                for (int i = k - start + 1; i < size; i += 2) {
                    int q = p - dp[i] - dp[i + 1] + 1;
                    // int q = p - dp[i] + 1;
                    for (int j = q; j <= p; ++j)
                        arr[m++] = j;
                    p = q - 1;
                }

                // print array
                assert(p == 0);
                assert(m == n);
                for (int i = 0; i < n; ++i) {
                    if (i) printf(" ");
                    printf("%d", arr[i]);
                }
                printf("\n");
                int ms = mergeSort(0, n);
                // cout << n << ' ' << k << ' ' << ms << endl;
                // cout << start << ' ' << k << ' ' << size << ' ' << k - start + 1 << ' ' << size << endl;
                assert(ms == k);
            } else {
                int s2 = size / 2;
                gen(s2);
                for (int i = 0; i < s2; ++i) dp[i] = 1;
                int cursize = s2;
                int ptr = s2 - 1;
                int total = start - 1;
                while (cursize < n && ptr >= 0) {
                    ++cursize;
                    dp[ord[ptr]] += 1;
                    total += 2;
                    --ptr;
                }
                // cout << cursize << ' ' << total << ' ' << ptr << endl;
                if (cursize != n || total != k || ptr < 0) {
                    printf("-1\n");
                } else {
                    int m = 0;
                    int p = n;
                    for (int i = 0; i < s2; ++i) {
                        if (dp[i] == 1) {
                            arr[m++] = p--;
                        } else {
                            arr[m++] = p - 0;
                            arr[m++] = p - 1;
                            p -= 2;
                        }
                    }
                    assert(p == 0);
                    assert(n == m);

                    for (int i = 0; i < n; ++i) {
                        if (i) printf(" ");
                        printf("%d", arr[i]);
                    }
                    printf("\n");

                    int ms = mergeSort(0, n);

                    // cout << ms << ' ' << k << endl;
                    assert(ms == k);
                }
            }

        }
// }
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1000000;

int n, k;
int lg[N];
// 3 => 1 => 2 => 0
// 7 => 3 => 5 => 1 => 6 => 2 => 4 => 0
// 111
// 011
// 101
// 001
// 110
// 010
// 100
// 000
int ord[N], dp[N], arr[N], tmp[N];

void gen(int size) {
    int bits = lg[size];
    for (int i = 0; i < size; ++i) {
        // flip bits
        int mask = i;
        int rev = 0;
        while (mask > 0) {
            int big = lg[mask];
            rev |= 1 << (bits - big - 1);
            mask ^= 1 << big;
        }
        ord[size - i - 1] = rev;
    }
}

int mergeSort(int L, int R) {
    int res = 1;
    bool need = false;
    for (int i = L + 1; i < R; ++i) {
        if (!(arr[i - 1] <= arr[i])) {
            need = true;
            break;
        }
    }
    if (need) {
        int M = (L + R) / 2;
        res += mergeSort(L, M) + mergeSort(M, R);
        merge(arr + L, arr + M, arr + M, arr + R, tmp);
        copy(tmp, tmp + (R - L), arr + L);
    } else {
        // static int ptr = 0;
        // cout << "Leaf: " << L << " " << R << " " << R-L << " " << dp[ptr++] << endl;
    }
    return res;
}

int main() {
    for (int i = 2; i < N; ++i) lg[i] = lg[i >> 1] + 1;
    scanf("%d%d", &n, &k);
    if (k % 2 == 0) printf("-1\n");
    else {
        int start = 1 << lg[k];
        int end = 2 << lg[k];
        int size = end - start;
        gen(size);
        for (int i = 0; i < size; ++i) dp[i] = 0;
        int cursize = 0;
        if (n >= size) {

            // allocate
            for (int i = 0; i < size; ++i) dp[i] = 1;

            // generate order
            gen(size);

            // add extra elements one by one
            int cursize = size;
            int ptr = 0;
            while (cursize < n) {
                dp[ord[ptr++]] += 1;
                ptr %= size;
                ++cursize;
            }

            // cout << "size: " <<  cursize << endl;

            // construct array
            int p = n;
            int m = 0;

            for (int i = 0; i < size&& start + i <= k; ++i) {
                int q = p - dp[i] + 1;
                for (int j = q; j <= p; ++j)
                    arr[m++] = j;
                p = q - 1;
            }
            // cout << "after: " << p << ' ' << end - k << ' ' << endl;

            for (int i = k - start + 1; i < size; i += 2) {
                int q = p - dp[i] - dp[i + 1] + 1;
                // int q = p - dp[i] + 1;
                for (int j = q; j <= p; ++j)
                    arr[m++] = j;
                p = q - 1;
            }

            // print array
            assert(p == 0);
            assert(m == n);
            for (int i = 0; i < n; ++i) {
                if (i) printf(" ");
                printf("%d", arr[i]);
            }
            printf("\n");
            int ms = mergeSort(0, n);
            // cout << n << ' ' << k << ' ' << ms << endl;
            // cout << start << ' ' << k << ' ' << size << ' ' << k - start + 1 << ' ' << size << endl;
            assert(ms == k);
        } else {
            int s2 = size / 2;
            gen(s2);
            for (int i = 0; i < s2; ++i) dp[i] = 1;
            int cursize = s2;
            int ptr = s2 - 1;
            int total = start - 1;
            while (cursize < n && ptr >= 0) {
                ++cursize;
                dp[ord[ptr]] += 1;
                total += 2;
                --ptr;
            }
            // cout << cursize << ' ' << total << ' ' << ptr << endl;
            if (cursize != n || total != k || ptr < 0) {
                printf("-1\n");
            } else {
                int m = 0;
                int p = n;
                for (int i = 0; i < s2; ++i) {
                    if (dp[i] == 1) {
                        arr[m++] = p--;
                    } else {
                        arr[m++] = p - 1;
                        arr[m++] = p - 0;
                        p -= 2;
                    }
                }
                assert(p == 0);
                assert(n == m);

                for (int i = 0; i < n; ++i) {
                    if (i) printf(" ");
                    printf("%d", arr[i]);
                }
                printf("\n");
                int ms = mergeSort(0, n);
                assert(ms == k);
            }
        }

    }
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1000000;

int n, k;
int lg[N];
// 3 => 1 => 2 => 0
// 7 => 3 => 5 => 1 => 6 => 2 => 4 => 0
// 111
// 011
// 101
// 001
// 110
// 010
// 100
// 000
int ord[N], dp[N], arr[N], tmp[N];

void gen(int size) {
    int bits = lg[size];
    for (int i = 0; i < size; ++i) {
        // flip bits
        int mask = i;
        int rev = 0;
        while (mask > 0) {
            int big = lg[mask];
            rev |= 1 << (bits - big - 1);
            mask ^= 1 << big;
        }
        ord[size - i - 1] = rev;
    }
}

int mergeSort(int L, int R) {
    int res = 1;
    bool need = false;
    for (int i = L + 1; i < R; ++i) {
        if (!(arr[i - 1] <= arr[i])) {
            need = true;
            break;
        }
    }
    if (need) {
        int M = (L + R) / 2;
        res += mergeSort(L, M) + mergeSort(M, R);
        merge(arr + L, arr + M, arr + M, arr + R, tmp);
        copy(tmp, tmp + (R - L), arr + L);
    } else {
        // static int ptr = 0;
        // cout << "Leaf: " << L << " " << R << " " << R-L << " " << dp[ptr++] << endl;
    }
    return res;
}

int main() {
    for (int i = 2; i < N; ++i) lg[i] = lg[i >> 1] + 1;
    scanf("%d%d", &n, &k);
    if (k % 2 == 0) printf("-1\n");
    else {
        int start = 1 << lg[k];
        int end = 2 << lg[k];
        int size = end - start;
        if (n + 1 < size) {
            printf("-1\n");
        } else if (n + 1 == size) {
            if (lg[k + 2] != lg[k]) {
                // last leaf cannot be succeeded
                printf("-1\n");
            } else {
                int m = 0;
                int p = n;
                for (int i = 0; i < size && start + i <= k; ++i) {
                    arr[m++] = p--;
                }
                for (int i = k - start + 1; i < size - 2; i += 2) {
                    arr[m++] = p - 1;
                    arr[m++] = p;
                    p -= 2;
                }
                arr[m++] = p--;
                assert(p == 0);
                assert(m == n);
                for (int i = 0; i < n; ++i) {
                    if (i) printf(" ");
                    printf("%d", arr[i]);
                }
                printf("\n");
                int ms = mergeSort(0, n);
                // cout << n << ' ' << k << ' ' << ms << endl;
                // cout << start << ' ' << k << ' ' << size << ' ' << k - start + 1 << ' ' << size << endl;
                assert(ms == k);
            }
        } else {

            // allocate
            for (int i = 0; i < size; ++i) dp[i] = 1;

            // generate order
            gen(size);

            // add extra elements one by one
            int cursize = size;
            int ptr = 0;
            while (cursize < n) {
                dp[ord[ptr++]] += 1;
                ptr %= size;
                ++cursize;
            }

            // cout << "size: " <<  cursize << endl;

            // construct array
            int p = n;
            int m = 0;

            for (int i = 0; i < size&& start + i <= k; ++i) {
                int q = p - dp[i] + 1;
                for (int j = q; j <= p; ++j)
                    arr[m++] = j;
                p = q - 1;
            }
            // cout << "after: " << p << ' ' << end - k << ' ' << endl;

            for (int i = k - start + 1; i < size; i += 2) {
                int q = p - dp[i] - dp[i + 1] + 1;
                // int q = p - dp[i] + 1;
                for (int j = q; j <= p; ++j)
                    arr[m++] = j;
                p = q - 1;
            }

            // print array
            assert(p == 0);
            assert(m == n);
            for (int i = 0; i < n; ++i) {
                if (i) printf(" ");
                printf("%d", arr[i]);
            }
            printf("\n");
            int ms = mergeSort(0, n);
            // cout << n << ' ' << k << ' ' << ms << endl;
            // cout << start << ' ' << k << ' ' << size << ' ' << k - start + 1 << ' ' << size << endl;
            assert(ms == k);
        }

    }
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1000000;

int n, k;
int lg[N];
// 3 => 1 => 2 => 0
// 7 => 3 => 5 => 1 => 6 => 2 => 4 => 0
// 111
// 011
// 101
// 001
// 110
// 010
// 100
// 000
int ord[N], dp[N], arr[N], tmp[N];

void gen(int size) {
    int bits = lg[size];
    for (int i = 0; i < size; ++i) {
        // flip bits
        int mask = i;
        int rev = 0;
        while (mask > 0) {
            int big = lg[mask];
            rev |= 1 << (bits - big - 1);
            mask ^= 1 << big;
        }
        ord[size - i - 1] = rev;
    }
}

int mergeSort(int L, int R) {
    int res = 1;
    bool need = false;
    for (int i = L + 1; i < R; ++i) {
        if (!(arr[i - 1] <= arr[i])) {
            need = true;
            break;
        }
    }
    if (need) {
        int M = (L + R) / 2;
        res += mergeSort(L, M) + mergeSort(M, R);
        merge(arr + L, arr + M, arr + M, arr + R, tmp);
        copy(tmp, tmp + (R - L), arr + L);
    } else {
        // static int ptr = 0;
        // cout << "Leaf: " << L << " " << R << " " << R-L << " " << dp[ptr++] << endl;
    }
    return res;
}

int main() {
    for (int i = 2; i < N; ++i) lg[i] = lg[i >> 1] + 1;
    scanf("%d%d", &n, &k);
    if (k % 2 == 0) printf("-1\n");
    else {
        int start = 1 << lg[k];
        int end = 2 << lg[k];
        int size = end - start;
        if (n < size) {
            printf("-1\n");
        } else {

            // allocate
            for (int i = 0; i < size; ++i) dp[i] = 1;

            // generate order
            gen(size);

            // add extra elements one by one
            int cursize = size;
            int ptr = 0;
            while (cursize < n) {
                dp[ord[ptr++]] += 1;
                ptr %= size;
                ++cursize;
            }

            // cout << "size: " <<  cursize << endl;

            // construct array
            int p = n;
            int m = 0;

            for (int i = 0; i < size&& start + i <= k; ++i) {
                int q = p - dp[i] + 1;
                for (int j = q; j <= p; ++j)
                    arr[m++] = j;
                p = q - 1;
            }
            // cout << "after: " << p << ' ' << end - k << ' ' << endl;

            for (int i = k - start + 1; i < size; i += 2) {
                int q = p - dp[i] - dp[i + 1] + 1;
                // int q = p - dp[i] + 1;
                for (int j = q; j <= p; ++j)
                    arr[m++] = j;
                p = q - 1;
            }

            // print array
            assert(p == 0);
            assert(m == n);
            for (int i = 0; i < n; ++i) {
                if (i) printf(" ");
                printf("%d", arr[i]);
            }
            printf("\n");
            int ms = mergeSort(0, n);
            // cout << n << ' ' << k << ' ' << ms << endl;
            // cout << start << ' ' << k << ' ' << size << ' ' << k - start + 1 << ' ' << size << endl;
            assert(ms == k);
        }

    }
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1000000;

int n, k;
int lg[N];
// 3 => 1 => 2 => 0
// 7 => 3 => 5 => 1 => 6 => 2 => 4 => 0
// 111
// 011
// 101
// 001
// 110
// 010
// 100
// 000
int ord[N], dp[N], arr[N], tmp[N];

void gen(int size) {
    int bits = lg[size];
    for (int i = 0; i < size; ++i) {
        // flip bits
        int mask = i;
        int rev = 0;
        while (mask > 0) {
            int big = lg[mask];
            rev |= 1 << (bits - big - 1);
            mask ^= 1 << big;
        }
        ord[size - i - 1] = rev;
    }
}

int mergeSort(int L, int R) {
    int res = 1;
    bool need = false;
    for (int i = L + 1; i < R; ++i) {
        if (!(arr[i - 1] <= arr[i])) {
            need = true;
            break;
        }
    }
    if (need) {
        int M = (L + R) / 2;
        // cout << "merge: " << L << ' ' << M << ' ' << R << endl;
        res += mergeSort(L, M) + mergeSort(M, R);
        merge(arr + L, arr + M, arr + M, arr + R, tmp);
        copy(tmp, tmp + (R - L + 1), arr + L);
    }
    return res;
}

int main() {
    for (int i = 2; i < N; ++i) lg[i] = lg[i >> 1] + 1;
    scanf("%d%d", &n, &k);
    if (k % 2 == 0 || n < lg[k]) printf("-1\n");
    else {
        int start = 1 << lg[k];
        int end = 2 << lg[k];
        int size = end - start;

        // allocate
        for (int i = 0; i < size; ++i) dp[i] = 1;

        // generate order
        gen(size);

        // add extra elements one by one
        int cursize = size;
        int ptr = 0;
        while (cursize < n) {
            dp[ord[ptr++]] += 1;
            ptr %= size;
            ++cursize;
        }

        // construct array
        int m = 0;
        int p = n;
        for (int i = 0; i < size; ++i) {
            int q = p - dp[i] + 1;
            for (int j = q; j <= p; ++j)
                arr[m++] = j;
            p = q - 1;
        }

        // print array
        assert(p == 0);
        assert(m == n);
        for (int i = 0; i < n; ++i) {
            if (i) printf(" ");
            printf("%d", arr[i]);
        }
        printf("\n");

        assert(mergeSort(0, n) == k);

    }
}


#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <utility>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <list>
#include <stack>
#include <bitset>
#include <cmath>
#include <climits>
#include <cassert>
#include <string.h>

using namespace std;

int a[100000];

int get_max_calls(int n) {
  int total = 0;
  while (n != 1) {
    if ((n & 1) != 0) {
      total += n - 1;
      n = (n - 1) / 2 + 1;
    } else {
      total += n;
      n /= 2;
    }
  }
  return total + 1;
}

bool solve(int l, int r, int k) {
  --k;
  if (k == 0) {
    return true;
  }
  if ((k & 1) != 0) {
    return false;
  }
  int mid = (l + r) / 2;
  int lmax = get_max_calls(mid - l);
  int rmax = get_max_calls(r - mid);
  if (lmax + rmax < k) {
    return false;
  }
  int tmp = a[mid - 1];
  a[mid - 1] = a[mid];
  a[mid] = tmp;
  return solve(l, mid, lmax) & solve(mid, r, k - lmax);
}

int main(int argc, char** argv) {
  std::ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    a[i] = i + 1;
  }

  bool ok = solve(0, n, k);
  if (!ok) {
    cout << "-1\n";
  } else {
    for (int i = 0; i < n - 1; ++i) {
      cout << a[i] << " ";
    }
    cout << a[n - 1] << "\n";
  }
}


#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <utility>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <list>
#include <stack>
#include <bitset>
#include <cmath>
#include <climits>
#include <cassert>
#include <string.h>

using namespace std;

int a[100000];

int get_max_calls(int n) {
  int total = 0;
  while (n != 1) {
    if ((n & 1) != 0) {
      total += n - 1;
      n = (n - 1) / 2 + 1;
    } else {
      total += n;
      n /= 2;
    }
  }
  return total + 1;
}

bool solve(int l, int r, int k) {
  --k;
  if (r == l + 1) {
    return k == 0;
  }
  if (k == 0) {
    return true;
  }
  if ((k & 1) != 0) {
    return false;
  }
  int mid = (l + r) / 2;
  int lmax = get_max_calls(mid - l);
  int rmax = get_max_calls(r - mid);
  if (lmax + rmax < k) {
    return false;
  }
  int tmp = a[mid - 1];
  a[mid - 1] = a[mid];
  a[mid] = tmp;
  int ln = lmax;
  if (ln > k) {
    ln = k - 1;
  }
  return solve(l, mid, ln) && solve(mid, r, k - ln);
}

int main(int argc, char** argv) {
  std::ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    a[i] = i + 1;
  }

  bool ok = solve(0, n, k);
  if (!ok) {
    cout << "-1\n";
  } else {
    for (int i = 0; i < n - 1; ++i) {
      cout << a[i] << " ";
    }
    cout << a[n - 1] << "\n";
  }
}


#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <utility>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <list>
#include <stack>
#include <bitset>
#include <cmath>
#include <climits>
#include <cassert>
#include <string.h>

using namespace std;

int a[100000];

int get_max_calls(int n) {
  int total = 0;
  while (n != 1) {
    if ((n & 1) != 0) {
      total += n - 1;
      n = (n - 1) / 2 + 1;
    } else {
      total += n;
      n /= 2;
    }
  }
  return total + 1;
}

bool solve(int l, int r, int k) {
  --k;
  if (r == l + 1) {
    return k == 0;
  }
  if (k == 0) {
    return true;
  }
  if ((k & 1) != 0) {
    return false;
  }
  int mid = (l + r) / 2;
  int lmax = get_max_calls(mid - l);
  int rmax = get_max_calls(r - mid);
  if (lmax + rmax < k) {
    return false;
  }
  int tmp = a[mid - 1];
  a[mid - 1] = a[mid];
  a[mid] = tmp;
  return solve(l, mid, lmax) && solve(mid, r, k - lmax);
}

int main(int argc, char** argv) {
  std::ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    a[i] = i + 1;
  }

  bool ok = solve(0, n, k);
  if (!ok) {
    cout << "-1\n";
  } else {
    for (int i = 0; i < n - 1; ++i) {
      cout << a[i] << " ";
    }
    cout << a[n - 1] << "\n";
  }
}


#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <utility>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <list>
#include <stack>
#include <bitset>
#include <cmath>
#include <climits>
#include <cassert>
#include <string.h>

using namespace std;

int a[100000];

int get_max_calls(int n) {
  int total = 0;
  while (n != 1) {
    if ((n & 1) != 0) {
      total += n - 1;
      n = (n - 1) / 2 + 1;
    } else {
      total += n;
      n /= 2;
    }
  }
  return total + 1;
}

bool solve(int l, int r, int k) {
  --k;
  if (r == l + 1) {
    return k == 0;
  }
  if ((k & 1) != 0) {
    return false;
  }
  int mid = (l + r) / 2;
  int lmax = get_max_calls(mid - l);
  int rmax = get_max_calls(r - mid);
  if (lmax + rmax < k) {
    return false;
  }
  int tmp = a[mid - 1];
  a[mid - 1] = a[mid];
  a[mid] = tmp;
  return solve(l, mid, lmax) && solve(mid, r, k - lmax);
}

int main(int argc, char** argv) {
  std::ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    a[i] = i + 1;
  }

  bool ok = solve(0, n, k);
  if (!ok) {
    cout << "-1\n";
  } else {
    for (int i = 0; i < n - 1; ++i) {
      cout << a[i] << " ";
    }
    cout << a[n - 1] << "\n";
  }
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
bool seen[100004];
int par[100004],child[100004],L[100004],root[100004],bap[100004][17];
int r;
stack<int>s;
bool seen2[100004];

void Topsort(int x)
{
    if(seen2[x])return;seen2[x]=true;
    for(int i=0;i<v[x].size();i++)Topsort(v[x][i]);
    s.push(x);
   /// cout<<"x = "<<x<<endl;
}
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    root[x]=r;
    L[x]=level;
    if(par[x]!=100002)
    child[par[x]]++;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
}
void counting(int n)
{
    memset(bap,-1,sizeof(bap));
    for(int i=0;i<n;i++)bap[i][0]=par[i];

    for(int i=1;(1<<i)<n;i++)
        for(int j=0;j<n;j++){
                if(bap[j][i-1]==-1)continue;
                bap[j][i]=bap[bap[j][i-1]][i-1];
                if(bap[j][i]==-1)bap[j][i]=100002;
        }
}
int go(int x,int diff)
{
    int log=log2(diff);
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0){x=bap[x][i];
        if(x==-1 || x==100002)break;
        }
    }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&x);x--;
        if(x>=0){v[x].push_back(i);
        par[i]=x;
        }
        else par[i]=100002;
    }
    for(int i=0;i<n;i++){
        if(seen2[i]==0)Topsort(i);
    }
    while(!s.empty())
    {
        int t=s.top();
        if(seen[t]==0){
        ///cout<<"go = "<<t<<endl;
        par[t]=100002,r=t;
        dfs(t,0);
        }
        s.pop();
    }
    ///for(int i=0;i<n;i++)cout<<"child of "<<i<<" = "<<child[i]<<endl;
    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        x--;
        int diff=L[x]-L[root[x]];
        if(p>diff)printf("0 ");
        else{
            int ans=go(x,diff+1);
            if(ans==-1 || ans==100002)printf("0 ");
            else{
                ans=child[par[x]]-1;
                printf("%d ",ans);
            }
        }
    }
    return 0;

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
vector<int>v2[100004];
bool seen[100004];
int par[100004],L[100004],root[100004],bap[100004][17];
int r,cnt=0,start=0;
stack<int>s;
bool seen2[100004];
int ST[100005],EN[100005];

void Topsort(int x)
{
    if(seen2[x])return;seen2[x]=true;
    for(int i=0;i<v[x].size();i++)Topsort(v[x][i]);
    s.push(x);
   /// cout<<"x = "<<x<<endl;
}
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    v2[level].push_back(start);
    ST[x]=start;start++;
    ///root[x]=r;
    L[x]=level;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
    EN[x]=start;
    start++;
}
void counting(int n)
{
    memset(bap,-1,sizeof(bap));
    for(int i=0;i<n;i++)bap[i][0]=par[i];

    for(int i=1;(1<<i)<n;i++)
        for(int j=0;j<n;j++){
                if(bap[j][i-1]==-1)continue;
                bap[j][i]=bap[bap[j][i-1]][i-1];
        }
}
int go(int x,int diff)
{
    int log=log2(diff);
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0){x=bap[x][i];
        if(x==-1 || x==100002)break;
        }
    }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&x);x--;
        if(x>=0){v[x].push_back(i);
        par[i]=x;
        }
        else par[i]=100002;
    }
    for(int i=0;i<n;i++){
        if(seen2[i]==0)Topsort(i);
    }
    while(!s.empty())
    {
        int t=s.top();
        if(seen[t]==0){
        ///cout<<"go = "<<t<<endl;
        par[t]=100002,r=t;
        dfs(t,0);
        }
        s.pop();
    }
    //for(int i=0;i<v2[1].size();i++)cout<<"L2 = "<<v2[1][i]<<endl;return 0;
    ///for(int i=0;i<n;i++)cout<<"size of  "<<i<<" = "<<SZ[i]<<endl;
    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        x--;
        int diff=L[x]-L[root[x]];
        if(p>diff)printf("0 ");
        else{
            int ans=go(x,diff+1);
            if(ans==-1 || ans==100002)printf("0 ");
            else{
                 ans=upper_bound(v2[L[x]].begin(),v2[L[x]].end(),EN[ans])-lower_bound(v2[L[x]].begin(),v2[L[x]].end(),ST[ans]);
                 printf("%d ",ans-1);
            }
        }
    }
    return 0;
    /*
    7
7 7 7 7 3  2 0
7
5 2

*/

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
vector<int>v2[100004];
bool seen[100004];
int par[100004],L[100004],root[100004],bap[100004][17];
int r,cnt=0,start=0;
stack<int>s;
bool seen2[100004];
int ST[100005],EN[100005];
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    v2[level].push_back(start);
    ST[x]=start;start++;
    ///root[x]=r;
    L[x]=level;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
    EN[x]=start;
    start++;
}
void counting(int n)
{
    memset(bap,-1,sizeof(bap));
    for(int i=0;i<n;i++)bap[i][0]=par[i];

    for(int i=1;(1<<i)<n;i++)
        for(int j=0;j<n;j++){
                bap[j][i]=bap[bap[j][i-1]][i-1];
        }
}
int go(int x,int diff)
{
    int log=log2(diff);
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0){x=bap[x][i];
        if(x==-1 || x==100002 || x==0)break;
        }
    }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&x);
        par[i+1]=x;
        v[x].push_back(i+1);
    }
    n++;
    for(int i=0;i<n;i++)if(seen[i]==0)
    dfs(i,0);
    ///for(int i=0;i<n;i++)cout<<"level of "<<i<<" = "<<L[i]<<endl;
    //for(int i=0;i<v2[1].size();i++)cout<<"L2 = "<<v2[1][i]<<endl;return 0;
    ///for(int i=0;i<n;i++)cout<<"size of  "<<i<<" = "<<SZ[i]<<endl;
    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        int ans=go(x,p);
        if(ans==-1 || ans==100002 || ans==0)printf("0 ");
        else
            {
                 ans=upper_bound(v2[L[x]].begin(),v2[L[x]].end(),EN[ans])-lower_bound(v2[L[x]].begin(),v2[L[x]].end(),ST[ans]);
                 printf("%d ",ans-1);
            }

    }
    return 0;
    /*
    7
7 7 7 7 3  2 0
7
5 2

*/

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
vector<int>v2[100004];
bool seen[100004];
int par[100004],child[100004],L[100004],root[100004],bap[100004][17],ara[100005];
int r,cnt=0;
stack<int>s;
bool seen2[100004];

void Topsort(int x)
{
    if(seen2[x])return;seen2[x]=true;
    for(int i=0;i<v[x].size();i++)Topsort(v[x][i]);
    if(x!=100002)
    s.push(x);
   /// cout<<"x = "<<x<<endl;
}
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    ara[x]=cnt;
    cnt++;
    root[x]=r;
    L[x]=level;
    if(x!=100002)
    v2[level].push_back(x);
    if(par[x]!=100002)
    child[par[x]]++;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
}
void counting(int n)
{
    memset(bap,-1,sizeof(bap));
    for(int i=0;i<n;i++)bap[i][0]=par[i];

    for(int i=1;(1<<i)<n;i++)
        for(int j=0;j<n;j++){
                if(bap[j][i-1]==-1)continue;
                bap[j][i]=bap[bap[j][i-1]][i-1];
                if(bap[j][i]==-1)bap[j][i]=100002;
        }
}
int go(int x,int diff)
{
    int log=log2(diff);
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0){x=bap[x][i];
        if(x==-1 || x==100002)break;
        }
    }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&x);x--;
        if(x>=0){v[x].push_back(i);
        par[i]=x;
        }
        else par[i]=100002;
    }
    for(int i=0;i<n;i++){
        if(seen2[i]==0)Topsort(i);
    }
    while(!s.empty())
    {
        int t=s.top();
        if(seen[t]==0 && t!=100002){
        ///cout<<"go = "<<t<<endl;
        par[t]=100002,r=t;
        dfs(t,0);
        }
        s.pop();
    }
    //for(int i=0;i<v2[1].size();i++)cout<<"L2 = "<<v2[1][i]<<endl;return 0;
    ///for(int i=0;i<n;i++)cout<<"child of "<<i<<" = "<<child[i]<<endl;
    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        x--;
        int diff=L[x]-L[root[x]];
        if(p>diff)printf("0 ");
        else{
            int ans=go(x,diff+1);
            if(ans==-1 || ans==100002)printf("0 ");
            else{
                ///ans=child[par[x]]-1;
                int Lcnt=L[x],sz=v2[Lcnt].size()-1;
                int low=0,high=sz,mid,ansL=-1,ansR=-1;
                while(low<=high)
                {
                    mid=(low+high)>>1;

                    if(root[v2[Lcnt][mid]]==root[x]){
                        ansL=mid;
                        ///cout<<"modeL = "<<v2[Lcnt][mid]<<endl;
                        high=mid-1;
                    }
                    else if(ara[v2[Lcnt][mid]]<ara[x])low=mid+1;
                    else high=mid-1;
                }
                low=0,high=sz;
                while(low<=high)
                {
                    mid=(low+high)>>1;
                    if(root[v2[Lcnt][mid]]==root[x]){
                        ansR=mid;
                        ///cout<<"modeR = "<<v2[Lcnt][mid]<<endl;
                        low=mid+1;
                    }
                    else if(ara[v2[Lcnt][mid]]<ara[x])low=mid+1;
                    else high=mid-1;
                }
                ans=ansR-ansL;
                printf("%d ",ans);

            }
        }
    }
    return 0;

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
vector<int>v2[100004];
bool seen[100004];
int par[100004],L[100004],root[100004],bap[100004][17],SZ[100006];
int r,cnt=1,start=0;
stack<int>s;
bool seen2[100004];
int ST[100005];///EN[100005];
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    SZ[x]=1;
    v2[level].push_back(start);
    ////A[start]=x;
    ST[x]=start;start++;
    ///root[x]=r;
    L[x]=level;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
   /// A[start]=x;
   SZ[par[x]]+=SZ[x];
}
void counting(int n)
{
    for(int i=1;i<=n;i++)bap[i][0]=par[i];

    for(int i=1;i<=16;i++)
        for(int j=1;j<=n;j++){
                bap[j][i]=bap[bap[j][i-1]][i-1];
        }
}
int go(int x,int diff)
{
    int log,ans=0;
    if(diff==0)log=0;
    else
     log=log2(diff);
     ///cout<<"log=  "<<log<<endl;
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0){x=bap[x][i];
        ///cout<<"xinside = "<<x<<endl;
        ans=x;
        }
    }
    return ans;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&x);

        par[i]=x;
        v[x].push_back(i);
    }
    for(int i=1;i<=n;i++)if(seen[i]==0 && par[i]==0)dfs(i,0);
   /// for(int i=1;i<=n;i++)cout<<"level of "<<i<<" = "<<L[i]<<endl;
    ///for(int i=1;i<=n;i++)cout<<"par of  "<<i<<" =  "<<par[i]<<endl;
    //for(int i=0;i<v2[1].size();i++)cout<<"L2 = "<<v2[1][i]<<endl;return 0;
   /// for(int i=1;i<=n;i++)cout<<"size of  "<<i<<" = "<<SZ[i]<<endl;
    ///cout<<"ber korlam = "<<go(2,2)<<endl;
    ///for(int i=0;i<v2[1].size();i++)cout<<"level 1 = "<<A[v2[1][i]]<<endl;

    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        int ans=go(x,p);
       /// cout<<"ans = "<<ans<<endl;
        if(ans==0)printf("0 ");
        else
            {
                 int r1=ST[ans],r2=ST[ans]+SZ[ans]-1;
                 int low=lower_bound(v2[L[x]].begin(),v2[L[x]].end(),r1)-v2[L[x]].begin();
                 int up=upper_bound(v2[L[x]].begin(),v2[L[x]].end(),r2)-v2[L[x]].begin();
                 ans=up-low;
                 ///cout<<"low = "<<low<<endl;
                 ///cout<<"low value = "<<v2[L[x]][low]<<endl;
                 printf("%d ",ans-1);
            }
    }

    return 0;
    /*
    7
7 7 7 7 3  2 0
7
5 2

*/

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
vector<int>v2[100004];
bool seen[100004];
int par[100004],L[100004],root[100004],bap[100004][17];
int r,cnt=0,start=0;
stack<int>s;
bool seen2[100004];
int ST[100005],EN[100005];
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    v2[level].push_back(start);
    ST[x]=start;start++;
    ///root[x]=r;
    L[x]=level;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
    EN[x]=start;
    start++;
}
void counting(int n)
{
    memset(bap,-1,sizeof(bap));
    for(int i=1;i<=n;i++)bap[i][0]=par[i];

    for(int i=1;(1<<i)<=n;i++)
        for(int j=1;j<=n;j++){
                if(bap[j][i-1]==-1)continue;
                bap[j][i]=bap[bap[j][i-1]][i-1];
        }
}
int go(int x,int diff)
{
    int log;
    if(diff==0)log=0;
    else
     log=log2(diff);
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0){x=bap[x][i];
        if(x==-1 || x==100002 || x==0)break;
        }
    }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&x);
        par[i+1]=x;
        v[x].push_back(i+1);
    }
    n++;
    for(int i=0;i<n;i++)if(seen[i]==0)
    dfs(i,0);
    ///for(int i=0;i<n;i++)cout<<"level of "<<i<<" = "<<L[i]<<endl;
    //for(int i=0;i<v2[1].size();i++)cout<<"L2 = "<<v2[1][i]<<endl;return 0;
    ///for(int i=0;i<n;i++)cout<<"size of  "<<i<<" = "<<SZ[i]<<endl;
    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        int ans=go(x,p);
        if(ans==-1 || ans==100002 || ans==0)printf("0 ");
        else
            {
                 ans=upper_bound(v2[L[x]].begin(),v2[L[x]].end(),EN[ans])-lower_bound(v2[L[x]].begin(),v2[L[x]].end(),ST[ans]);
                 printf("%d ",ans-1);
            }

    }
    return 0;
    /*
    7
7 7 7 7 3  2 0
7
5 2

*/

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
vector<int>v2[100004];
bool seen[100004];
int par[100004],L[100004],root[100004],bap[100004][17];
int r,cnt=0,start=0;
stack<int>s;
bool seen2[100004];
int ST[100005],EN[100005];

void Topsort(int x)
{
    if(seen2[x])return;seen2[x]=true;
    for(int i=0;i<v[x].size();i++)Topsort(v[x][i]);
    s.push(x);
   /// cout<<"x = "<<x<<endl;
}
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    v2[level].push_back(start);
    ST[x]=start;start++;
    ///root[x]=r;
    L[x]=level;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
    EN[x]=start;
    start++;
}
void counting(int n)
{
    memset(bap,-1,sizeof(bap));
    for(int i=0;i<n;i++)bap[i][0]=par[i];

    for(int i=1;(1<<i)<n;i++)
        for(int j=0;j<n;j++){
                if(bap[j][i-1]==-1)continue;
                bap[j][i]=bap[bap[j][i-1]][i-1];
        }
}
int go(int x,int diff)
{
    int log=log2(diff);
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0){x=bap[x][i];
        if(x==-1 || x==100002)break;
        }
    }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&x);x--;
        if(x>=0){v[x].push_back(i);
        par[i]=x;
        }
        else{ par[i]=100002;
          v[100002].push_back(i);
        }
    }
    for(int i=0;i<n;i++){
        if(seen2[i]==0)Topsort(i);
    }
    while(!s.empty())
    {
        int t=s.top();
        if(seen[t]==0){
        ///cout<<"go = "<<t<<endl;
        par[t]=100002,r=t;
        dfs(t,0);
        }
        s.pop();
    }
    //for(int i=0;i<v2[1].size();i++)cout<<"L2 = "<<v2[1][i]<<endl;return 0;
    ///for(int i=0;i<n;i++)cout<<"size of  "<<i<<" = "<<SZ[i]<<endl;
    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        x--;
        int diff=L[x]-L[root[x]];
        if(p>diff)printf("0 ");
        else{
            int ans=go(x,diff);
            if(ans==-1 || ans==100002)printf("0 ");
            else{
                 ans=upper_bound(v2[L[x]].begin(),v2[L[x]].end(),EN[ans])-lower_bound(v2[L[x]].begin(),v2[L[x]].end(),ST[ans]);
                 printf("%d ",ans-1);
            }
        }
    }
    return 0;
    /*
    7
7 7 7 7 3  2 0
7
5 2

*/

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
vector<int>v2[100004];
bool seen[100004];
int par[100004],child[100004],L[100004],root[100004],bap[100004][17],ara[100005];
int r,cnt=0,start=0;
stack<int>s;
bool seen2[100004];
int SZ[100005],ST[100005];

void Topsort(int x)
{
    if(seen2[x])return;seen2[x]=true;
    for(int i=0;i<v[x].size();i++)Topsort(v[x][i]);
    if(x!=100002)
    s.push(x);
   /// cout<<"x = "<<x<<endl;
}
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    ST[x]=start;start++;
    SZ[x]=1;
    ara[x]=cnt;
    cnt++;
    root[x]=r;
    L[x]=level;
    if(x!=100002)
    v2[level].push_back(x);
    if(par[x]!=100002)
    child[par[x]]++;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
    if(x!=r)SZ[par[x]]+=SZ[x];
    start++;
}
void counting(int n)
{
    memset(bap,-1,sizeof(bap));
    for(int i=0;i<n;i++)bap[i][0]=par[i];

    for(int i=1;(1<<i)<n;i++)
        for(int j=0;j<n;j++){
                if(bap[j][i-1]==-1)continue;
                bap[j][i]=bap[bap[j][i-1]][i-1];
                if(bap[j][i]==-1)bap[j][i]=100002;
        }
}
int go(int x,int diff)
{
    int log=log2(diff);
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0){x=bap[x][i];
        if(x==-1 || x==100002)break;
        }
    }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&x);x--;
        if(x>=0){v[x].push_back(i);
        par[i]=x;
        }
        else par[i]=100002;
    }
    for(int i=0;i<n;i++){
        if(seen2[i]==0)Topsort(i);
    }
    while(!s.empty())
    {
        int t=s.top();
        if(seen[t]==0 && t!=100002){
        ///cout<<"go = "<<t<<endl;
        par[t]=100002,r=t;
        dfs(t,0);
        }
        s.pop();
    }
    //for(int i=0;i<v2[1].size();i++)cout<<"L2 = "<<v2[1][i]<<endl;return 0;
    ///for(int i=0;i<n;i++)cout<<"size of  "<<i<<" = "<<SZ[i]<<endl;
    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        x--;
        int diff=L[x]-L[root[x]];
        if(p>diff)printf("0 ");
        else{
            int ans=go(x,diff+1);
            if(ans==-1 || ans==100002)printf("0 ");
            else{
                ///ans=child[par[x]]-1;
                ///cout<<"ST of root "<<ans<<" = "<<ST[ans]<<endl;
                int Lcnt=L[x],sz=v2[Lcnt].size()-1;
                int low=0,high=sz,mid,ansL=-1,ansR=-1;
                while(low<=high)
                {
                    mid=(low+high)>>1;
                    ///cout <<"STL of mid "<<v2[Lcnt][mid]<<" = "<<ST[v2[Lcnt][mid]]<<endl;
                    if(root[v2[Lcnt][mid]]==root[x] && ST[v2[Lcnt][mid]]>ST[ans] && ST[v2[Lcnt][mid]]<=(ST[ans]+SZ[ans]+SZ[ans])){
                        ansL=mid;
                        ///cout<<"modeL = "<<v2[Lcnt][mid]<<endl;
                        high=mid-1;
                    }
                    else if(ara[v2[Lcnt][mid]]<ara[x])low=mid+1;
                    else high=mid-1;
                }
                low=0,high=sz;
                while(low<=high)
                {
                    mid=(low+high)>>1;
                    ///cout <<"STR of mid "<<v2[Lcnt][mid]<<" = "<<ST[v2[Lcnt][mid]]<<endl;
                     if(root[v2[Lcnt][mid]]==root[x] && ST[v2[Lcnt][mid]]>ST[ans] && ST[v2[Lcnt][mid]]<=(ST[ans]+SZ[ans]+SZ[ans])){
                        ansR=mid;
                        ///cout<<"modeR = "<<v2[Lcnt][mid]<<endl;
                        low=mid+1;
                    }
                    else if(ara[v2[Lcnt][mid]]<ara[x])low=mid+1;
                    else high=mid-1;
                }
                ans=ansR-ansL;
                printf("%d ",ans);

            }
        }
    }
    return 0;
    /*
    7
7 7 7 7 3  2 0
7
5 2

*/

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
vector<int>v2[100004];
bool seen[100004];
int par[100004],L[100004],root[100004],bap[100004][17];
int r,cnt=0,start=0;
stack<int>s;
bool seen2[100004];
int ST[100005],EN[100005];
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    v2[level].push_back(start);
    ST[x]=start;start++;
    ///root[x]=r;
    L[x]=level;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
    EN[x]=start;
    start++;
}
void counting(int n)
{
    memset(bap,-1,sizeof(bap));
    for(int i=1;i<=n;i++)bap[i][0]=par[i];

    for(int i=1;(1<<i)<=n;i++)
        for(int j=1;j<=n;j++){
                bap[j][i]=bap[bap[j][i-1]][i-1];
        }
}
int go(int x,int diff)
{
    int log=log2(diff);
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0){x=bap[x][i];
        if(x==-1 || x==100002 || x==0)break;
        }
    }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&x);
        par[i+1]=x;
        v[x].push_back(i+1);
    }
    n++;
    for(int i=0;i<n;i++)if(seen[i]==0)
    dfs(i,0);
    ///for(int i=0;i<n;i++)cout<<"level of "<<i<<" = "<<L[i]<<endl;
    //for(int i=0;i<v2[1].size();i++)cout<<"L2 = "<<v2[1][i]<<endl;return 0;
    ///for(int i=0;i<n;i++)cout<<"size of  "<<i<<" = "<<SZ[i]<<endl;
    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        int ans=go(x,p);
        if(ans==-1 || ans==100002 || ans==0)printf("0 ");
        else
            {
                 ans=upper_bound(v2[L[x]].begin(),v2[L[x]].end(),EN[ans])-lower_bound(v2[L[x]].begin(),v2[L[x]].end(),ST[ans]);
                 printf("%d ",ans-1);
            }

    }
    return 0;
    /*
    7
7 7 7 7 3  2 0
7
5 2

*/

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
vector<int>v2[100004];
bool seen[100004];
int par[100004],child[100004],L[100004],root[100004],bap[100004][17],ara[100005];
int r,cnt=0;
stack<int>s;
bool seen2[100004];

void Topsort(int x)
{
    if(seen2[x])return;seen2[x]=true;
    for(int i=0;i<v[x].size();i++)Topsort(v[x][i]);
    s.push(x);
   /// cout<<"x = "<<x<<endl;
}
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    ara[x]=cnt;
    cnt++;
    root[x]=r;
    L[x]=level;
    v2[level].push_back(x);
    if(par[x]!=100002)
    child[par[x]]++;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
}
void counting(int n)
{
    memset(bap,-1,sizeof(bap));
    for(int i=0;i<n;i++)bap[i][0]=par[i];

    for(int i=1;(1<<i)<n;i++)
        for(int j=0;j<n;j++){
                if(bap[j][i-1]==-1)continue;
                bap[j][i]=bap[bap[j][i-1]][i-1];
                if(bap[j][i]==-1)bap[j][i]=100002;
        }
}
int go(int x,int diff)
{
    int log=log2(diff);
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0){x=bap[x][i];
        if(x==-1 || x==100002)break;
        }
    }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&x);x--;
        if(x>=0){v[x].push_back(i);
        par[i]=x;
        }
        else par[i]=100002;
    }
    for(int i=0;i<n;i++){
        if(seen2[i]==0)Topsort(i);
    }
    while(!s.empty())
    {
        int t=s.top();
        if(seen[t]==0){
        ///cout<<"go = "<<t<<endl;
        par[t]=100002,r=t;
        dfs(t,0);
        }
        s.pop();
    }
    ///for(int i=0;i<n;i++)cout<<"child of "<<i<<" = "<<child[i]<<endl;
    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        x--;
        int diff=L[x]-L[root[x]];
        if(p>diff)printf("0 ");
        else{
            int ans=go(x,diff+1);
            if(ans==-1 || ans==100002)printf("0 ");
            else{
                ///ans=child[par[x]]-1;
                int Lcnt=L[x],sz=v2[Lcnt].size()-1;
                int low=0,high=sz,mid,ansL=-1,ansR=-1;
                while(low<=high)
                {
                    mid=(low+high)>>1;
                    if(root[v2[Lcnt][mid]]==root[x]){
                        ansL=mid;
                        high=mid-1;
                    }
                    else if(ara[v2[Lcnt][mid]]<ara[x])low=mid+1;
                    else high=mid-1;
                }
                low=0,high=sz;
                while(low<=high)
                {
                    mid=(low+high)>>1;
                    if(root[v2[Lcnt][mid]]==root[x]){
                        ansR=mid;
                        low=mid+1;
                    }
                    else if(ara[v2[Lcnt][mid]]<ara[x])low=mid+1;
                    else high=mid-1;
                }
                ans=ansR-ansL;
                printf("%d ",ans);

            }
        }
    }
    return 0;

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
vector<int>v2[100004];
bool seen[100004];
int par[100004],L[100004],root[100004],bap[100004][17];
int r,cnt=0,start=0;
stack<int>s;
bool seen2[100004];
int ST[100005],EN[100005];
void TopSort(int x)
{
    if(seen2[x])return;seen2[x]=true;
    for(int i=0;i<v[x].size();i++)TopSort(v[x][i]);
    s.push(x);
}
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    v2[level].push_back(start);
    ////A[start]=x;
    ST[x]=start;start++;
    ///root[x]=r;
    L[x]=level;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
   /// A[start]=x;
    EN[x]=start;
    start++;
}
void counting(int n)
{
    for(int i=1;i<=n;i++)bap[i][0]=par[i];

    for(int i=1;(1<<i)<=n;i++)
        for(int j=1;j<=n;j++){
                bap[j][i]=bap[bap[j][i-1]][i-1];
        }
}
int go(int x,int diff)
{
    int log;
    if(diff==0)log=0;
    else
     log=log2(diff);
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0){x=bap[x][i];
        if(x==0)break;
        }
    }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        par[i]=x;
        if(x!=0)
        v[x].push_back(i);
    }
    for(int i=1;i<=n;i++){
        if(seen2[i]==0)TopSort(i);
    }
       while(!s.empty())
       {
           int t=s.top();
           if(seen[t]==0){dfs(t,0);
           }
           s.pop();
       }
    ///for(int i=1;i<=n;i++)cout<<"level of "<<i<<" = "<<L[i]<<endl;
    //for(int i=0;i<v2[1].size();i++)cout<<"L2 = "<<v2[1][i]<<endl;return 0;
    ///for(int i=0;i<n;i++)cout<<"size of  "<<i<<" = "<<SZ[i]<<endl;
    ///cout<<"ber korlam = "<<go(2,2)<<endl;
    ///for(int i=0;i<v2[1].size();i++)cout<<"level 1 = "<<A[v2[1][i]]<<endl;

    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        if(L[x]<p)printf("0 ");
        else{
        int ans=go(x,p);
        ///cout<<"ans = "<<ans<<endl;
        if(ans==0)printf("0 ");
        else
            {
                 int low=lower_bound(v2[L[x]].begin(),v2[L[x]].end(),ST[ans])-v2[L[x]].begin();
                 int up=upper_bound(v2[L[x]].begin(),v2[L[x]].end(),EN[ans])-v2[L[x]].begin();
                 ans=up-low;
                 ///cout<<"low = "<<low<<endl;
                 ///cout<<"low value = "<<v2[L[x]][low]<<endl;
                 printf("%d ",ans-1);
            }

        }

    }
    return 0;
    /*
    7
7 7 7 7 3  2 0
7
5 2

*/

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
vector<int>v2[100004];
bool seen[100004];
int par[100004],L[100004],root[100004],bap[100004][17];
int r,cnt=0,start=0;
stack<int>s;
bool seen2[100004];
int ST[100005],EN[100005];

void Topsort(int x)
{
    if(seen2[x])return;seen2[x]=true;
    for(int i=0;i<v[x].size();i++)Topsort(v[x][i]);
    s.push(x);
   /// cout<<"x = "<<x<<endl;
}
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    v2[level].push_back(start);
    ST[x]=start;start++;
    ///root[x]=r;
    L[x]=level;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
    EN[x]=start;
    start++;
}
void counting(int n)
{
    memset(bap,-1,sizeof(bap));
    for(int i=0;i<n;i++)bap[i][0]=par[i];

    for(int i=1;(1<<i)<n;i++)
        for(int j=0;j<n;j++){
                if(bap[j][i-1]==-1)continue;
                bap[j][i]=bap[bap[j][i-1]][i-1];
        }
}
int go(int x,int diff)
{
    int log=log2(diff);
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0){x=bap[x][i];
        if(x==-1 || x==100002)break;
        }
    }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&x);x--;
        if(x>=0){v[x].push_back(i);
        par[i]=x;
        }
        else{ par[i]=100002;
          v[100002].push_back(i);
        }
    }
    for(int i=0;i<n;i++){
        if(seen2[i]==0)Topsort(i);
    }
    while(!s.empty())
    {
        int t=s.top();
        if(seen[t]==0){
        ///cout<<"go = "<<t<<endl;
        par[t]=100002,r=t;
        dfs(t,0);
        }
        s.pop();
    }
    //for(int i=0;i<v2[1].size();i++)cout<<"L2 = "<<v2[1][i]<<endl;return 0;
    ///for(int i=0;i<n;i++)cout<<"size of  "<<i<<" = "<<SZ[i]<<endl;
    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        x--;
        int diff=L[x]-L[root[x]];
        if(p>diff)printf("0 ");
        else{
            int ans=go(x,diff+1);
            if(ans==-1 || ans==100002)printf("0 ");
            else{
                 ans=upper_bound(v2[L[x]].begin(),v2[L[x]].end(),EN[ans])-lower_bound(v2[L[x]].begin(),v2[L[x]].end(),ST[ans]);
                 printf("%d ",ans-1);
            }
        }
    }
    return 0;
    /*
    7
7 7 7 7 3  2 0
7
5 2

*/

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
vector<int>v2[100004];
bool seen[100004];
int par[100004],L[100004],root[100004],bap[100004][17],SZ[100006];
int r,cnt=1,start=0;
stack<int>s;
bool seen2[100004];
int ST[100005];///EN[100005];
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    SZ[x]=1;
    v2[level].push_back(start);
    ////A[start]=x;
    ST[x]=start;start++;
    ///root[x]=r;
    L[x]=level;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
   /// A[start]=x;
   SZ[par[x]]+=SZ[x];
}
void counting(int n)
{
    for(int i=1;i<=n;i++)bap[i][0]=par[i];

    for(int i=1;i<=16;i++)
        for(int j=1;j<=n;j++){
                bap[j][i]=bap[bap[j][i-1]][i-1];
        }
}
int go(int x,int diff)
{
    int log;
    if(diff==0)log=0;
    else
     log=log2(diff);
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0){x=bap[x][i];
        }
    }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        par[i]=x;
        v[x].push_back(i);
    }
    for(int i=0;i<=n;i++)if(seen[i]==0)dfs(i,0);
   /// for(int i=1;i<=n;i++)cout<<"level of "<<i<<" = "<<L[i]<<endl;
    //for(int i=0;i<v2[1].size();i++)cout<<"L2 = "<<v2[1][i]<<endl;return 0;
   /// for(int i=1;i<=n;i++)cout<<"size of  "<<i<<" = "<<SZ[i]<<endl;
    ///cout<<"ber korlam = "<<go(2,2)<<endl;
    ///for(int i=0;i<v2[1].size();i++)cout<<"level 1 = "<<A[v2[1][i]]<<endl;

    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        int ans=go(x,p);
        ///cout<<"ans = "<<ans<<endl;
        if(ans==0)printf("0 ");
        else
            {
                 int r1=ST[ans],r2=ST[ans]+SZ[ans]-1;
                 int low=lower_bound(v2[L[x]].begin(),v2[L[x]].end(),r1)-v2[L[x]].begin();
                 int up=upper_bound(v2[L[x]].begin(),v2[L[x]].end(),r2)-v2[L[x]].begin();
                 ans=up-low;
                 ///cout<<"low = "<<low<<endl;
                 ///cout<<"low value = "<<v2[L[x]][low]<<endl;
                 printf("%d ",ans-1);
            }
    }

    return 0;
    /*
    7
7 7 7 7 3  2 0
7
5 2

*/

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
vector<int>v2[100004];
bool seen[100004];
int par[100004],L[100004],root[100004],bap[100004][17];
int r,cnt=0,start=0;
stack<int>s;
bool seen2[100004];
int ST[100005],EN[100005];

void Topsort(int x)
{
    if(seen2[x])return;seen2[x]=true;
    for(int i=0;i<v[x].size();i++)Topsort(v[x][i]);
    if(x!=100002)
    s.push(x);
   /// cout<<"x = "<<x<<endl;
}
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    v2[level].push_back(start);
    ST[x]=start;start++;
    ///root[x]=r;
    L[x]=level;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
    EN[x]=start;
    start++;
}
void counting(int n)
{
    memset(bap,-1,sizeof(bap));
    for(int i=0;i<n;i++)bap[i][0]=par[i];

    for(int i=1;(1<<i)<n;i++)
        for(int j=0;j<n;j++){
                if(bap[j][i-1]==-1)continue;
                bap[j][i]=bap[bap[j][i-1]][i-1];
                if(bap[j][i]==-1)bap[j][i]=100002;
        }
}
int go(int x,int diff)
{
    int log=log2(diff);
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0){x=bap[x][i];
        if(x==-1 || x==100002)break;
        }
    }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&x);x--;
        if(x>=0){v[x].push_back(i);
        par[i]=x;
        }
        else par[i]=100002;
    }
    for(int i=0;i<n;i++){
        if(seen2[i]==0)Topsort(i);
    }
    while(!s.empty())
    {
        int t=s.top();
        if(seen[t]==0 && t!=100002){
        ///cout<<"go = "<<t<<endl;
        par[t]=100002,r=t;
        dfs(t,0);
        }
        s.pop();
    }
    //for(int i=0;i<v2[1].size();i++)cout<<"L2 = "<<v2[1][i]<<endl;return 0;
    ///for(int i=0;i<n;i++)cout<<"size of  "<<i<<" = "<<SZ[i]<<endl;
    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        x--;
        int diff=L[x]-L[root[x]];
        if(p>diff)printf("0 ");
        else{
            int ans=go(x,diff+1);
            if(ans==-1 || ans==100002)printf("0 ");
            else{
                 ans=upper_bound(v2[L[x]].begin(),v2[L[x]].end(),EN[ans])-lower_bound(v2[L[x]].begin(),v2[L[x]].end(),ST[ans]);
                 printf("%d ",ans-1);
            }
        }
    }
    return 0;
    /*
    7
7 7 7 7 3  2 0
7
5 2

*/

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
vector<int>v2[100004];
bool seen[100004];
int par[100004],child[100004],L[100004],root[100004],bap[100004][17],ara[100005];
int r,cnt=0,start=0;
stack<int>s;
bool seen2[100004];
int SZ[100005],ST[100005];

void Topsort(int x)
{
    if(seen2[x])return;seen2[x]=true;
    for(int i=0;i<v[x].size();i++)Topsort(v[x][i]);
    if(x!=100002)
    s.push(x);
   /// cout<<"x = "<<x<<endl;
}
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    ST[x]=start;start++;
    SZ[x]=1;
    ara[x]=cnt;
    cnt++;
    root[x]=r;
    L[x]=level;
    if(x!=100002)
    v2[level].push_back(x);
    if(par[x]!=100002)
    child[par[x]]++;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
    if(x!=r)SZ[par[x]]+=SZ[x];
}
void counting(int n)
{
    memset(bap,-1,sizeof(bap));
    for(int i=0;i<n;i++)bap[i][0]=par[i];

    for(int i=1;(1<<i)<n;i++)
        for(int j=0;j<n;j++){
                if(bap[j][i-1]==-1)continue;
                bap[j][i]=bap[bap[j][i-1]][i-1];
                if(bap[j][i]==-1)bap[j][i]=100002;
        }
}
int go(int x,int diff)
{
    int log=log2(diff);
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0){x=bap[x][i];
        if(x==-1 || x==100002)break;
        }
    }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&x);x--;
        if(x>=0){v[x].push_back(i);
        par[i]=x;
        }
        else par[i]=100002;
    }
    for(int i=0;i<n;i++){
        if(seen2[i]==0)Topsort(i);
    }
    while(!s.empty())
    {
        int t=s.top();
        if(seen[t]==0 && t!=100002){
        ///cout<<"go = "<<t<<endl;
        par[t]=100002,r=t;
        dfs(t,0);
        }
        s.pop();
    }
    //for(int i=0;i<v2[1].size();i++)cout<<"L2 = "<<v2[1][i]<<endl;return 0;
    ///for(int i=0;i<n;i++)cout<<"child of "<<i<<" = "<<child[i]<<endl;
    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        x--;
        int diff=L[x]-L[root[x]];
        if(p>diff)printf("0 ");
        else{
            int ans=go(x,diff+1);
            if(ans==-1 || ans==100002)printf("0 ");
            else{
                ///ans=child[par[x]]-1;
                ///cout<<"ST of root "<<ans<<" = "<<ST[ans]<<endl;
                int Lcnt=L[x],sz=v2[Lcnt].size()-1;
                int low=0,high=sz,mid,ansL=-1,ansR=-1;
                while(low<=high)
                {
                    mid=(low+high)>>1;
                    ///cout <<"STL of mid "<<v2[Lcnt][mid]<<" = "<<ST[v2[Lcnt][mid]]<<endl;
                    if(root[v2[Lcnt][mid]]==root[x] && ST[v2[Lcnt][mid]]>ST[ans] && ST[v2[Lcnt][mid]]<=(ST[ans]+SZ[ans]+SZ[ans])){
                        ansL=mid;
                        ///cout<<"modeL = "<<v2[Lcnt][mid]<<endl;
                        high=mid-1;
                    }
                    else if(ara[v2[Lcnt][mid]]<ara[x])low=mid+1;
                    else high=mid-1;
                }
                low=0,high=sz;
                while(low<=high)
                {
                    mid=(low+high)>>1;
                    ///cout <<"STR of mid "<<v2[Lcnt][mid]<<" = "<<ST[v2[Lcnt][mid]]<<endl;
                     if(root[v2[Lcnt][mid]]==root[x] && ST[v2[Lcnt][mid]]>ST[ans] && ST[v2[Lcnt][mid]]<=(ST[ans]+SZ[ans]+SZ[ans])){
                        ansR=mid;
                        ///cout<<"modeR = "<<v2[Lcnt][mid]<<endl;
                        low=mid+1;
                    }
                    else if(ara[v2[Lcnt][mid]]<ara[x])low=mid+1;
                    else high=mid-1;
                }
                ans=ansR-ansL;
                printf("%d ",ans);

            }
        }
    }
    return 0;
    /*
    7
7 7 7 7 3  2 0
7
5 2

*/

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
bool seen[100004];
int par[100004],child[100004],L[100004],root[100004],bap[100004][17];
int r;
stack<int>s;
bool seen2[100003];

void Topsort(int x)
{
    if(seen2[x])return;seen2[x]=true;
    for(int i=0;i<v[x].size();i++)Topsort(v[x][i]);
    s.push(x);
   /// cout<<"x = "<<x<<endl;
}
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    root[x]=r;
    L[x]=level;
    child[par[x]]++;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
}
void counting(int n)
{
    memset(bap,-1,sizeof(bap));
    for(int i=0;i<n;i++)bap[i][0]=par[i];

    for(int i=1;(1<<i)<n;i++)
        for(int j=0;j<n;j++)bap[j][i]=bap[bap[j][i-1]][i-1];
}
int go(int x,int diff)
{
    int log=log2(diff);
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0)x=bap[x][i];
    }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&x);x--;
        if(x>=0){v[x].push_back(i);
        par[i]=x;
        }
        else par[i]=100002;
    }
    for(int i=0;i<n;i++){
        if(seen2[i]==0)Topsort(i);
    }
    while(!s.empty())
    {
        int t=s.top();
        if(seen[t]==0){
        ///cout<<"go = "<<t<<endl;
        par[t]=100002,r=t;
        dfs(t,0);
        }
        s.pop();
    }
    ///for(int i=0;i<n;i++)cout<<"child of "<<i<<" = "<<child[i]<<endl;
    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        x--;
        int diff=L[x]-L[root[x]];
        if(p>diff)printf("0 ");
        else{
            int ans=go(x,diff+1);
            if(ans==-1 || ans==100002)printf("0 ");
            else{
                ans=child[par[x]]-1;
                printf("%d ",ans);
            }
        }
    }
    return 0;

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
vector<int>v2[100004];
bool seen[100004];
int par[100004],L[100004],root[100004],bap[100004][17],SZ[100006];
int r,cnt=1,start=0;
stack<int>s;
bool seen2[100004];
int ST[100005];///EN[100005];
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    SZ[x]=1;
    v2[level].push_back(start);
    ////A[start]=x;
    ST[x]=start;start++;
    ///root[x]=r;
    L[x]=level;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
   /// A[start]=x;
   SZ[par[x]]+=SZ[x];
}
void counting(int n)
{
    for(int i=1;i<=n;i++)bap[i][0]=par[i];

    for(int i=1;i<=16;i++)
        for(int j=1;j<=n;j++){
                bap[j][i]=bap[bap[j][i-1]][i-1];
        }
}
int go(int x,int diff)
{
    int log,ans=0;
     while(diff!=0){
        log=log2(diff);
        x=bap[x][log];
        diff-=(1<<log);
     }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&x);

        par[i]=x;
        v[x].push_back(i);
    }
    for(int i=1;i<=n;i++)if(seen[i]==0 && par[i]==0)dfs(i,0);
   /// for(int i=1;i<=n;i++)cout<<"level of "<<i<<" = "<<L[i]<<endl;
    ///for(int i=1;i<=n;i++)cout<<"par of  "<<i<<" =  "<<par[i]<<endl;
    //for(int i=0;i<v2[1].size();i++)cout<<"L2 = "<<v2[1][i]<<endl;return 0;
   /// for(int i=1;i<=n;i++)cout<<"size of  "<<i<<" = "<<SZ[i]<<endl;
    ///cout<<"ber korlam = "<<go(2,2)<<endl;
    ///for(int i=0;i<v2[1].size();i++)cout<<"level 1 = "<<A[v2[1][i]]<<endl;
    ///cout<<"bal 2  = "<<bap[2][1]<<endl;
    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        int ans=go(x,p);
       /// cout<<"ans = "<<ans<<endl;
        if(ans==0)printf("0 ");
        else
            {
                 int r1=ST[ans],r2=ST[ans]+SZ[ans]-1;
                 int low=lower_bound(v2[L[x]].begin(),v2[L[x]].end(),r1)-v2[L[x]].begin();
                 int up=upper_bound(v2[L[x]].begin(),v2[L[x]].end(),r2)-v2[L[x]].begin();
                 ans=up-low;
                 ///cout<<"low = "<<low<<endl;
                 ///cout<<"low value = "<<v2[L[x]][low]<<endl;
                 printf("%d ",ans-1);
            }
    }

    return 0;
    /*
    7
7 7 7 7 3  2 0
7
5 2

*/

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
vector<int>v2[100004];
bool seen[100004];
int par[100004],L[100004],root[100004],bap[100004][17],SZ[100006];
int r,cnt=1,start=0;
stack<int>s;
bool seen2[100004];
int ST[100005];///EN[100005];
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    SZ[x]=1;
    v2[level].push_back(start);
    ////A[start]=x;
    ST[x]=start;start++;
    ///root[x]=r;
    L[x]=level;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
   /// A[start]=x;
   SZ[par[x]]+=SZ[x];
}
void counting(int n)
{
    for(int i=1;i<=n;i++)bap[i][0]=par[i];

    for(int i=1;i<=16;i++)
        for(int j=1;j<=n;j++){
                bap[j][i]=bap[bap[j][i-1]][i-1];
        }
}
int go(int x,int diff)
{
    int log;
    if(diff==0)log=0;
    else
     log=log2(diff);
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0){x=bap[x][i];
        }
    }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        if(i!=x){
        par[i]=x;
        v[x].push_back(i);
        }
    }
    for(int i=0;i<=n;i++)if(seen[i]==0)dfs(i,0);
   /// for(int i=1;i<=n;i++)cout<<"level of "<<i<<" = "<<L[i]<<endl;
    //for(int i=0;i<v2[1].size();i++)cout<<"L2 = "<<v2[1][i]<<endl;return 0;
   /// for(int i=1;i<=n;i++)cout<<"size of  "<<i<<" = "<<SZ[i]<<endl;
    ///cout<<"ber korlam = "<<go(2,2)<<endl;
    ///for(int i=0;i<v2[1].size();i++)cout<<"level 1 = "<<A[v2[1][i]]<<endl;

    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        int ans=go(x,p);
        ///cout<<"ans = "<<ans<<endl;
        if(ans==0)printf("0 ");
        else
            {
                 int r1=ST[ans],r2=ST[ans]+SZ[ans]-1;
                 int low=lower_bound(v2[L[x]].begin(),v2[L[x]].end(),r1)-v2[L[x]].begin();
                 int up=upper_bound(v2[L[x]].begin(),v2[L[x]].end(),r2)-v2[L[x]].begin();
                 ans=up-low;
                 ///cout<<"low = "<<low<<endl;
                 ///cout<<"low value = "<<v2[L[x]][low]<<endl;
                 printf("%d ",ans-1);
            }
    }

    return 0;
    /*
    7
7 7 7 7 3  2 0
7
5 2

*/

}