#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <numeric>
#include <ctime>
#include <climits>
using namespace std;

int nextInt() {
	int x;
	scanf("%d", &x);
	return x;
}

long long nextLong() {
	long long x;
	scanf("%I64d", &x);
	return x;
}

double nextDouble() {
	double x;
	scanf("%lf", &x);
	return x;
}

const int BUFSIZE = 1000000;
char buf[BUFSIZE + 1];
string nextString() {
	scanf("%s", buf);
	return buf;
}
string nextLine() {
	gets(buf);
	return buf;
}

int stringToInt(string s) {
	stringstream in(s);
	int x;
	in >> x;
	return x;
}

struct Point {
	typedef double T;
	T x, y;
	Point () : x(0), y(0) {}
	Point (T x, T y) : x(x), y(y) {}
	Point operator - (Point op) const { return Point(x - op.x, y - op.y); }
	Point operator + (Point op) const { return Point(x + op.x, y + op.y); }
	Point operator * (T op) const { return Point(x * op, y * op); }
	T operator * (Point op) const { return x * op.x + y * op.y; }
	T operator % (Point op) const { return x * op.y - y * op.x; }
	T length2() { return x * x + y * y; }
	double length() { return sqrt(length2()); }
};

Point nextPoint() {
	double x = nextDouble();
	double y = nextDouble();
	return Point(x, y);
}

typedef vector<vector<int> > Adj;

vector<int> getPos(vector<int>& a) {
	vector<int> res(a.size());
	for (int i = 0; i < a.size(); ++i) {
		res[a[i]] = i;
	}
	return res;
}

vector<int> solveBrute(vector<int> a, vector<int> b) {
	int n = a.size();
	vector<int> posa = getPos(a);
	vector<int> posb = getPos(b);
	vector<int> res(n);
	for (int shift = 0; shift < n; ++shift) {
		int best = n;
		for (int i = 0; i < n; ++i) {
			int x = posa[i];
			int y = (posb[i] + shift) % n;
			int cur = abs(x - y);
			best = min(best, cur);
		}
		res[shift] = best;
	}
	return res;
}

int _n, _shift;

struct Element {
	int x0;
	int x1;
	Element(int x0, int x1) : x0(x0), x1(x1) {
	}
	bool operator < (const Element& other) const {
		int d1 = getDev();
		int d2 = other.getDev();
		if (d1 != d2) {
			return d1 < d2;
		}
		return x0 < other.x0;
	}
	bool operator == (const Element& other) const {
		return x0 == other.x0 && x1 == other.x1;
	}
	int getDev() const {
		return (x1 + _shift) % _n - x0;
	}
};

void addElement(const Element& e, set<Element>& pos, set<Element>& neg) {
	int dev = e.getDev();
	if (dev >= 0) {
		pos.insert(e);
	} else {
		neg.insert(e);
	}
}

int getOptimalDev(set<Element>& pos, set<Element>& neg) {
	int res = _n;
	if (!pos.empty()) {
		res = min(res, pos.begin()->getDev());
	}
	if (!neg.empty()) {
		res = min(res, -(--neg.end())->getDev());
	}
	return res;
}

void removeElement(Element& e, set<Element>& pos, set<Element>& neg) {
	set<Element>::iterator it = pos.find(e);
	if (it != pos.end() && *it == e) {
		pos.erase(it);
	} else {
		it = neg.find(e);
		neg.erase(it);
	}
}

vector<int> solve(vector<int> a, vector<int> b) {
	int n = a.size();
	vector<int> posa = getPos(a);
	vector<int> posb = getPos(b);
	_n = n;
	_shift = 0;
	set<Element> neg;
	set<Element> pos;
	for (int i = 0; i < n; ++i) {
		Element e(posa[i], posb[i]);
		addElement(e, pos, neg);
	}
	vector<int> res(n);
	res[0] = getOptimalDev(pos, neg);
	vector<vector<int> > moveToPos(n);
	for (int i = 0; i < n; ++i) {
		if (posa[i] != 0) {
			moveToPos[(posa[i] - posb[i] + n) % n].push_back(i);
		}
	}
	for (int i = 1; i < n; ++i) {
		int x = b[n - i];
		Element e(posa[x], posb[x]);
		removeElement(e, pos, neg);
		for (int j = 0; j < moveToPos[i].size(); ++j) {
			int x = moveToPos[i][j];
			Element e(posa[x], posb[x]);
			removeElement(e, pos, neg);
		}
		_shift = i;
		addElement(e, pos, neg);
		res[i] = getOptimalDev(pos, neg);
		for (int j = 0; j < moveToPos[i].size(); ++j) {
			int x = moveToPos[i][j];
			Element e(posa[x], posb[x]);
			addElement(e, pos, neg);
		}
	}
	return res;
}

int main() {
	int n = nextInt();
	vector<int> a(n);
	for (int i = 0; i < n; ++i) {
		a[i] = nextInt() - 1;
	}
	vector<int> b(n);
	for (int i = 0; i < n; ++i) {
		b[i] = nextInt() - 1;
	}
	vector<int> res = solve(a, b);
	for (int i = 0; i < res.size(); ++i) {
		cout << res[i] << endl;
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;

#define NMAX 100000

typedef map<int,int> mymap;

mymap bst;//���ڭȦh`i`
int N,A[NMAX],B[NMAX],pos_in_a[NMAX];

int ABS(int x) {
	return x>=0?x:-x;
}

void rotate(int OLD,int NEW) {
	mymap::iterator it;
	it=bst.find(OLD-pos_in_a[B[OLD]]);
	if(it==bst.end()) {
		puts("die");
		exit(1);
	}
	if(--(it->second)==0)bst.erase(it);
	++bst[N-1-pos_in_a[B[OLD]]+NEW];
}

void solve() {
	int i; mymap::iterator it;
	for(i=0;i<N;i++) {
		int ans=N;
		it=bst.lower_bound(i);
		if(it!=bst.end())ans=it->first-i;
		if(it!=bst.begin()){--it;ans=min(ans,-(it->first-i));}
		printf("%d\n",ans);
		if(i!=N-1)rotate(i,i+1);
	}
}

void build() {
	for(int i=0;i<N;i++)++bst[i-pos_in_a[B[i]]];
}

void input(int arr[],bool flag) {
	for(int i=0;i<N;i++) {
		scanf("%d",arr+i);arr[i]--;
		if(flag)pos_in_a[arr[i]]=i;
	}
}

int main() {
	scanf("%d",&N);
	input(A,true);
	input(B,false);
	build();
	solve();
	return 0;
}


#include <vector>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <memory.h>
#include <string>
#include <set>
#include <map>

using namespace std;

int a1[100005],a2[100005];
int w1[100005],w2[100005];

pair<int,int> mtree[500005];
int d[500005];

void update(int i,int l,int r,int pos,int add) {
    if (d[i]) {
        mtree[i].first+=d[i];
        d[2*i]+=d[i]; d[2*i+1]+=d[i]; d[i]=0;
    }
    if (l==r) {
        mtree[i].first=add;
        mtree[i].second=l;
        return;
    }
    int m=(l+r)>>1;
    if (pos<=m) update(2*i,l,m,pos,add);
    else update(2*i+1,m+1,r,pos,add);
    int val1=mtree[2*i].first+d[2*i],val2=mtree[2*i+1].first+d[2*i+1];
//  if (val1<=val2) mtree[i]=mtree[2*i];
//  else mtree[i]=mtree[2*i+1];
    mtree[i]=make_pair(min(val1,val2),val1<=val2 ? mtree[2*i].second : mtree[2*i+1].second);
}

int ans[100005];

//pair <int,int> fen[100005];

int main() {
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
    pair <int,int> WW=make_pair(2000000000,-1);
    for(int i=0;i<500000;++i) mtree[i]=WW;
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;++i) {
        scanf("%d",&a1[i]);
        w1[--a1[i]]=i;
        ans[i]=2000000000;
    }
    for(int i=0;i<n;++i) {
        scanf("%d",&a2[i]);
        w2[--a2[i]]=i;
    }
    for(int i=0;i<n;++i) update(1,0,n,i,w1[i]<=w2[i] ? (w2[i]-w1[i]) : 2000000000);
    for(int i=0;i<n;++i) {
        if (d[1]) {
            mtree[1].first+=d[1];
            d[2]+=d[1]; d[3]+=d[1]; d[1]=0;
        }
        while(mtree[1].first<0) {
            update(1,0,n,mtree[1].second,2000000000);
        }
        ans[i]=mtree[1].first;
        --d[1];
        update(1,0,n,a2[i],n-1-w1[a2[i]]);
    }

    for(int i=0;i<500000;++i) mtree[i]=WW;
    memset(d,0,sizeof(d));
    for(int i=0;i<n;++i) update(1,0,n,i,w1[i]>=w2[i] ? (w1[i]-w2[i]) : 2000000000);
    int pos=0;
    for(int i=0;i<n;++i) {
        if (d[1]) {
            mtree[1].first+=d[1];
            d[2]+=d[1]; d[3]+=d[1]; d[1]=0;
        }
        while(mtree[1].first<0) {
            update(1,0,n,mtree[1].second,2000000000);
        }
        ans[pos]=min(ans[pos],mtree[1].first);
        --d[1];
        update(1,0,n,a2[n-1-i],w1[a2[n-1-i]]);
        pos--;
        if (pos<0) pos+=n;
    }
/*  cout << 100000 << endl;
    for(int i=0;i<100000;++i)
        cout << i+1 << "    ";
    cout << endl;
    for(int i=0;i<100000;++i)
        cout << 100000-i << "   ";
    cout << endl;*/
    for(int i=0;i<n;++i)
        printf("%d\n",ans[i]);
    return 0;
}


#include <vector>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <memory.h>
#include <string>
#include <set>
#include <map>

using namespace std;

int a1[100005],a2[100005];
int w1[100005],w2[100005];

pair<int,int> mtree[400005];
int d[400005];

void update(int i,int l,int r,int pos,int add) {
    if (d[i]) {
        mtree[i].first+=d[i];
        d[2*i]+=d[i]; d[2*i+1]+=d[i]; d[i]=0;
    }
    if (l==r) {
        mtree[i].first=add;
        mtree[i].second=l;
        return;
    }
    int m=(l+r)>>1;
    if (pos<=m) update(2*i,l,m,pos,add);
    else update(2*i+1,m+1,r,pos,add);
    int val1=mtree[2*i].first+d[2*i],val2=mtree[2*i+1].first+d[2*i+1];
//  if (val1<=val2) mtree[i]=mtree[2*i];
//  else mtree[i]=mtree[2*i+1];
    mtree[i]=make_pair(min(val1,val2),val1<=val2 ? mtree[2*i].second : mtree[2*i+1].second);
}

int ans[100005];

//pair <int,int> fen[100005];

int main() {
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
    pair <int,int> WW=make_pair(2100000000,-1);
    for(int i=0;i<400000;++i) mtree[i]=WW;
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;++i) {
        scanf("%d",&a1[i]);
        w1[--a1[i]]=i;
        ans[i]=2000000000;
    }
    for(int i=0;i<n;++i) {
        scanf("%d",&a2[i]);
        w2[--a2[i]]=i;
    }
    for(int i=0;i<n;++i) update(1,0,n,i,w1[i]<=w2[i] ? (w2[i]-w1[i]) : 2000000000);
    for(int i=0;i<n;++i) {
        if (d[1]) {
            mtree[1].first+=d[1];
            d[2]+=d[1]; d[3]+=d[1]; d[1]=0;
        }
        while(mtree[1].first<0) {
            update(1,0,n,mtree[1].second,2000000000);
        }
        ans[i]=mtree[1].first;
        --d[1];
        update(1,0,n,a2[i],n-1-w1[a2[i]]);
    }

    for(int i=0;i<400000;++i) mtree[i]=WW;
    memset(d,0,sizeof(d));
    for(int i=0;i<n;++i) update(1,0,n,i,w1[i]>=w2[i] ? (w1[i]-w2[i]) : 2000000000);
    int pos=0;
    for(int i=0;i<n;++i) {
        if (d[1]) {
            mtree[1].first+=d[1];
            d[2]+=d[1]; d[3]+=d[1]; d[1]=0;
        }
        while(mtree[1].first<0) {
            update(1,0,n,mtree[1].second,2000000000);
        }
        ans[pos]=min(ans[pos],mtree[1].first);
        --d[1];
        update(1,0,n,a2[n-1-i],w1[a2[n-1-i]]);
        pos--;
        if (pos<0) pos+=n;
    }
/*  cout << 100000 << endl;
    for(int i=0;i<100000;++i)
        cout << i+1 << "    ";
    cout << endl;
    for(int i=0;i<100000;++i)
        cout << 100000-i << "   ";
    cout << endl;*/
    for(int i=0;i<n;++i)
        printf("%d\n",ans[i]);
    return 0;
}


#include <vector>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <memory.h>
#include <string>
#include <set>
#include <map>

using namespace std;

int a1[100005],a2[100005];
int w1[100005],w2[100005];

pair<int,int> mtree[400005];
int d[400005];

void update(int i,int l,int r,int pos,int add) {
    if (d[i]) {
        mtree[i].first+=d[i];
        d[2*i]+=d[i]; d[2*i+1]+=d[i]; d[i]=0;
    }
    if (l==r) {
        mtree[i].first=add;
        mtree[i].second=l;
        return;
    }
    int m=(l+r)>>1;
    if (pos<=m) update(2*i,l,m,pos,add);
    else update(2*i+1,m+1,r,pos,add);
    int val1=mtree[2*i].first+d[2*i],val2=mtree[2*i+1].first+d[2*i+1];
//  if (val1<=val2) mtree[i]=mtree[2*i];
//  else mtree[i]=mtree[2*i+1];
    mtree[i]=make_pair(min(val1,val2),val1<=val2 ? mtree[2*i].second : mtree[2*i+1].second);
}

int ans[100005];

int main() {
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
    pair <int,int> WW=make_pair(2100000000,-1);
    for(int i=0;i<400000;++i) mtree[i]=WW;
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;++i) {
        scanf("%d",&a1[i]);
        w1[--a1[i]]=i;
        ans[i]=2000000000;
    }
    for(int i=0;i<n;++i) {
        scanf("%d",&a2[i]);
        w2[--a2[i]]=i;
    }
    for(int i=0;i<n;++i) update(1,0,n-1,i,w1[i]<=w2[i] ? (w2[i]-w1[i]) : 2000000000);
    for(int i=0;i<n;++i) {
        if (d[1]) {
            mtree[1].first+=d[1];
            d[2]+=d[1]; d[3]+=d[1]; d[1]=0;
        }
        while(mtree[1].first<0) {
            update(1,0,n-1,mtree[1].second,2000000000);
        }
        ans[i]=mtree[1].first;
        --d[1];
        update(1,0,n-1,a2[i],n-1-w1[a2[i]]);
    }

    for(int i=0;i<400000;++i) mtree[i]=WW;
    memset(d,0,sizeof(d));
    for(int i=0;i<n;++i) update(1,0,n-1,i,w1[i]>=w2[i] ? (w1[i]-w2[i]) : 2000000000);
    int pos=0;
    for(int i=0;i<n;++i) {
        if (d[1]) {
            mtree[1].first+=d[1];
            d[2]+=d[1]; d[3]+=d[1]; d[1]=0;
        }
        while(mtree[1].first<0) {
            update(1,0,n-1,mtree[1].second,2000000000);
        }
        ans[pos]=min(ans[pos],mtree[1].first);
        --d[1];
        update(1,0,n-1,a2[n-1-i],w1[a2[n-1-i]]);
        pos--;
        if (pos<0) pos+=n;
    }
/*  cout << 100000 << endl;
    for(int i=0;i<100000;++i)
        cout << i+1 << "    ";
    cout << endl;
    for(int i=0;i<100000;++i)
        cout << 100000-i << "   ";
    cout << endl;*/
    for(int i=0;i<n;++i)
        printf("%d\n",ans[i]);
    return 0;
}


#include <vector>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <memory.h>
#include <string>
#include <set>
#include <map>

using namespace std;

int a1[100005],a2[100005];
int w1[100005],w2[100005];

pair<int,int> mtree[500005];
int d[500005];

void update(int i,int l,int r,int pos,int add) {
    if (d[i]) {
        mtree[i].first+=d[i];
        d[2*i]+=d[i]; d[2*i+1]+=d[i]; d[i]=0;
    }
    if (l==r) {
        mtree[i].first=add;
        mtree[i].second=l;
        return;
    }
    int m=(l+r)>>1;
    if (pos<=m) update(2*i,l,m,pos,add);
    else update(2*i+1,m+1,r,pos,add);
    int val1=mtree[2*i].first+d[2*i],val2=mtree[2*i+1].first+d[2*i+1];
//  if (val1<=val2) mtree[i]=mtree[2*i];
//  else mtree[i]=mtree[2*i+1];
    mtree[i]=make_pair(min(val1,val2),val1<=val2 ? mtree[2*i].second : mtree[2*i+1].second);
}

int ans[100005];

//pair <int,int> fen[100005];

int main() {
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
    pair <int,int> WW=make_pair(2100000000,-1);
    for(int i=0;i<500000;++i) mtree[i]=WW;
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;++i) {
        scanf("%d",&a1[i]);
        w1[--a1[i]]=i;
        ans[i]=2000000000;
    }
    for(int i=0;i<n;++i) {
        scanf("%d",&a2[i]);
        w2[--a2[i]]=i;
    }
    for(int i=0;i<n;++i) update(1,0,n,i,w1[i]<=w2[i] ? (w2[i]-w1[i]) : 2000000000);
    for(int i=0;i<n;++i) {
        if (d[1]) {
            mtree[1].first+=d[1];
            d[2]+=d[1]; d[3]+=d[1]; d[1]=0;
        }
        while(mtree[1].first<0) {
            update(1,0,n,mtree[1].second,2000000000);
        }
        ans[i]=mtree[1].first;
        --d[1];
        update(1,0,n,a2[i],n-1-w1[a2[i]]);
    }

    for(int i=0;i<500000;++i) mtree[i]=WW;
    memset(d,0,sizeof(d));
    for(int i=0;i<n;++i) update(1,0,n,i,w1[i]>=w2[i] ? (w1[i]-w2[i]) : 2000000000);
    int pos=0;
    for(int i=0;i<n;++i) {
        if (d[1]) {
            mtree[1].first+=d[1];
            d[2]+=d[1]; d[3]+=d[1]; d[1]=0;
        }
        while(mtree[1].first<0) {
            update(1,0,n,mtree[1].second,2000000000);
        }
        ans[pos]=min(ans[pos],mtree[1].first);
        --d[1];
        update(1,0,n,a2[n-1-i],w1[a2[n-1-i]]);
        pos--;
        if (pos<0) pos+=n;
    }
/*  cout << 100000 << endl;
    for(int i=0;i<100000;++i)
        cout << i+1 << "    ";
    cout << endl;
    for(int i=0;i<100000;++i)
        cout << 100000-i << "   ";
    cout << endl;*/
    for(int i=0;i<n;++i)
        printf("%d\n",ans[i]);
    return 0;
}


#include <vector>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <memory.h>
#include <string>
#include <set>
#include <map>

using namespace std;

int a1[100005],a2[100005];
int w1[100005],w2[100005];

pair<int,int> mtree[500005];
int d[500005];

void update(int i,int l,int r,int pos,int add) {
    if (l==r) {
        mtree[i].first=add;
        mtree[i].second=l;
        d[i]=0;
        return;
    }
    if (d[i]) {
        mtree[i].first+=d[i];
        d[2*i]+=d[i]; d[2*i+1]+=d[i]; d[i]=0;
    }
    int m=(l+r)>>1;
    if (pos<=m) update(2*i,l,m,pos,add);
    else update(2*i+1,m+1,r,pos,add);
    int val1=mtree[2*i].first+d[2*i],val2=mtree[2*i+1].first+d[2*i+1];
//  if (val1<=val2) mtree[i]=mtree[2*i];
//  else mtree[i]=mtree[2*i+1];
    mtree[i]=make_pair(min(val1,val2),val1<=val2 ? mtree[2*i].second : mtree[2*i+1].second);
}

int ans[100005];

//pair <int,int> fen[100005];

int main() {
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
    pair <int,int> WW=make_pair(1000000000,-1);
    for(int i=0;i<500000;++i) mtree[i]=WW;
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;++i) {
        scanf("%d",&a1[i]);
        w1[--a1[i]]=i;
        ans[i]=1000000000;
    }
    for(int i=0;i<n;++i) {
        scanf("%d",&a2[i]);
        w2[--a2[i]]=i;
    }
    for(int i=0;i<n;++i) update(1,0,n,i,w1[i]<=w2[i] ? (w2[i]-w1[i]) : 1000000000);
    for(int i=0;i<n;++i) {
        if (d[1]) {
            mtree[1].first+=d[1];
            d[2]+=d[1]; d[3]+=d[1]; d[1]=0;
        }
        while(mtree[1].first<0) {
            update(1,0,n,mtree[1].second,1000000000);
        }
        ans[i]=mtree[1].first;
        --d[1];
        update(1,0,n,a2[i],n-1-w1[a2[i]]);
    }

    for(int i=0;i<500000;++i) mtree[i]=WW;
    memset(d,0,sizeof(d));
    for(int i=0;i<n;++i) update(1,0,n,i,w1[i]>=w2[i] ? (w1[i]-w2[i]) : 1000000000);
    int pos=0;
    for(int i=0;i<n;++i) {
        if (d[1]) {
            mtree[1].first+=d[1];
            d[2]+=d[1]; d[3]+=d[1]; d[1]=0;
        }
        while(mtree[1].first<0) {
            update(1,0,n,mtree[1].second,1000000000);
        }
        ans[pos]=min(ans[pos],mtree[1].first);
        --d[1];
        update(1,0,n,a2[n-1-i],w1[a2[n-1-i]]);
        pos--;
        if (pos<0) pos+=n;
    }
/*  cout << 100000 << endl;
    for(int i=0;i<100000;++i)
        cout << i+1 << "    ";
    cout << endl;
    for(int i=0;i<100000;++i)
        cout << 100000-i << "   ";
    cout << endl;*/
    for(int i=0;i<n;++i)
        printf("%d\n",ans[i]);
    return 0;
}


#include <vector>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <memory.h>
#include <string>
#include <set>
#include <map>

using namespace std;

int a1[100005],a2[100005];
int w1[100005],w2[100005];

pair<int,int> mtree[400005];
int d[400005];

void update(int i,int l,int r,int L,int R,int add) {
    if (d[i]) {
        mtree[i].first+=d[i];
        d[2*i]+=d[i]; d[2*i+1]+=d[i]; d[i]=0;
    }
    if (l==L&&r==R) {
        if (l==r) {
            mtree[i].first=add;
            mtree[i].second=l;
        } else d[i]+=add;
        return;
    }
    int m=(l+r)>>1;
    if (R<=m) update(2*i,l,m,L,R,add);
    else if (L>m) update(2*i+1,m+1,r,L,R,add);
    else {
        update(2*i,l,m,L,m,add);
        update(2*i+1,m+1,r,m+1,R,add);
    }
    int val1=mtree[2*i].first+d[2*i],val2=mtree[2*i+1].first+d[2*i+1];
    if (val1<=val2) mtree[i]=mtree[2*i];
    else mtree[i]=mtree[2*i+1];
    mtree[i].first=min(val1,val2);
}

int ans[100005];

int main() {
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
    for(int i=0;i<400000;++i) mtree[i]=make_pair(2100000000,-1);
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;++i) {
        scanf("%d",&a1[i]);
        w1[--a1[i]]=i;
        ans[i]=2000000000;
    }
    for(int i=0;i<n;++i) {
        scanf("%d",&a2[i]);
        w2[--a2[i]]=i;
    }
    for(int i=0;i<n;++i) update(1,0,n-1,i,i,w1[i]<=w2[i] ? (w2[i]-w1[i]) : 2000000000);
    for(int i=0;i<n;++i) {
        if (d[1]) {
            mtree[1].first+=d[1];
            d[2]+=d[1]; d[3]+=d[1]; d[1]=0;
        }
        pair <int,int> p=mtree[1];
        while(p.first<0) {
            update(1,0,n-1,p.second,p.second,2000000000);
            if (d[1]) {
                mtree[1].first+=d[1];
                d[2]+=d[1]; d[3]+=d[1]; d[1]=0;
            }
            p=mtree[1];
        }
        update(1,0,n-1,0,n-1,-1);
        update(1,0,n-1,a2[i],a2[i],n-1-w1[a2[i]]);
        ans[i]=p.first;
    }

    for(int i=0;i<400000;++i) mtree[i]=make_pair(2100000000,-1);
    for(int i=0;i<n;++i) update(1,0,n-1,i,i,w1[i]>=w2[i] ? (w1[i]-w2[i]) : 2000000000);
    int pos=0;
    for(int i=0;i<n;++i) {
        if (d[1]) {
            mtree[1].first+=d[1];
            d[2]+=d[1]; d[3]+=d[1]; d[1]=0;
        }
        pair <int,int> p=mtree[1];
        while(p.first<0) {
            update(1,0,n-1,p.second,p.second,2000000000);
            if (d[1]) {
                mtree[1].first+=d[1];
                d[2]+=d[1]; d[3]+=d[1]; d[1]=0;
            }
            p=mtree[1];
        }
        update(1,0,n-1,0,n-1,-1);
        update(1,0,n-1,a2[n-1-i],a2[n-1-i],w1[a2[n-1-i]]);
        ans[pos]=min(ans[pos],p.first);
        pos--;
        if (pos<0) pos+=n;
    }
    for(int i=0;i<n;++i)
        printf("%d\n",ans[i]);
    return 0;
}


#include <vector>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <memory.h>
#include <string>
#include <set>
#include <map>

using namespace std;

int a1[100005],a2[100005];
int w1[100005],w2[100005];

pair<int,int> mtree[500005];
int d[500005];

void update(int i,int l,int r,int pos,int add) {
    if (d[i]) {
        mtree[i].first+=d[i];
        d[2*i]+=d[i]; d[2*i+1]+=d[i]; d[i]=0;
    }
    if (l==r) {
        mtree[i].first=add;
        mtree[i].second=l;
        return;
    }
    int m=(l+r)>>1;
    if (pos<=m) update(2*i,l,m,pos,add);
    else update(2*i+1,m+1,r,pos,add);
    int val1=mtree[2*i].first+d[2*i],val2=mtree[2*i+1].first+d[2*i+1];
//  if (val1<=val2) mtree[i]=mtree[2*i];
//  else mtree[i]=mtree[2*i+1];
    mtree[i]=make_pair(min(val1,val2),val1<=val2 ? mtree[2*i].second : mtree[2*i+1].second);
}

int ans[100005];

//pair <int,int> fen[100005];

int main() {
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
    pair <int,int> WW=make_pair(1000000000,-1);
    for(int i=0;i<500000;++i) mtree[i]=WW;
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;++i) {
        scanf("%d",&a1[i]);
        w1[--a1[i]]=i;
        ans[i]=1000000000;
    }
    for(int i=0;i<n;++i) {
        scanf("%d",&a2[i]);
        w2[--a2[i]]=i;
    }
    for(int i=0;i<n;++i) update(1,0,n,i,w1[i]<=w2[i] ? (w2[i]-w1[i]) : 1000000000);
    for(int i=0;i<n;++i) {
        if (d[1]) {
            mtree[1].first+=d[1];
            d[2]+=d[1]; d[3]+=d[1]; d[1]=0;
        }
        while(mtree[1].first<0) {
            update(1,0,n,mtree[1].second,1000000000);
        }
        ans[i]=mtree[1].first;
        --d[1];
        update(1,0,n,a2[i],n-1-w1[a2[i]]);
    }

    for(int i=0;i<500000;++i) mtree[i]=WW;
    memset(d,0,sizeof(d));
    for(int i=0;i<n;++i) update(1,0,n,i,w1[i]>=w2[i] ? (w1[i]-w2[i]) : 1000000000);
    int pos=0;
    for(int i=0;i<n;++i) {
        if (d[1]) {
            mtree[1].first+=d[1];
            d[2]+=d[1]; d[3]+=d[1]; d[1]=0;
        }
        while(mtree[1].first<0) {
            update(1,0,n,mtree[1].second,1000000000);
        }
        ans[pos]=min(ans[pos],mtree[1].first);
        --d[1];
        update(1,0,n,a2[n-1-i],w1[a2[n-1-i]]);
        pos--;
        if (pos<0) pos+=n;
    }
/*  cout << 100000 << endl;
    for(int i=0;i<100000;++i)
        cout << i+1 << "    ";
    cout << endl;
    for(int i=0;i<100000;++i)
        cout << 100000-i << "   ";
    cout << endl;*/
    for(int i=0;i<n;++i)
        printf("%d\n",ans[i]);
    return 0;
}


#include <vector>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <memory.h>
#include <string>
#include <set>
#include <map>

using namespace std;

int a1[100005],a2[100005];
int w1[100005],w2[100005];

pair<int,int> mtree[500005];
int d[500005];

void update(int i,int l,int r,int pos,int add) {
    if (l==r) {
        mtree[i].first=add;
        mtree[i].second=l;
        return;
    }
    if (d[i]) {
        mtree[i].first+=d[i];
        d[2*i]+=d[i]; d[2*i+1]+=d[i]; d[i]=0;
    }
    int m=(l+r)>>1;
    if (pos<=m) update(2*i,l,m,pos,add);
    else update(2*i+1,m+1,r,pos,add);
    int val1=mtree[2*i].first+d[2*i],val2=mtree[2*i+1].first+d[2*i+1];
//  if (val1<=val2) mtree[i]=mtree[2*i];
//  else mtree[i]=mtree[2*i+1];
    mtree[i]=make_pair(min(val1,val2),val1<=val2 ? mtree[2*i].second : mtree[2*i+1].second);
}

int ans[100005];

//pair <int,int> fen[100005];

int main() {
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
    pair <int,int> WW=make_pair(1000000000,-1);
    for(int i=0;i<500000;++i) mtree[i]=WW;
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;++i) {
        scanf("%d",&a1[i]);
        w1[--a1[i]]=i;
        ans[i]=1000000000;
    }
    for(int i=0;i<n;++i) {
        scanf("%d",&a2[i]);
        w2[--a2[i]]=i;
    }
    for(int i=0;i<n;++i) update(1,0,n,i,w1[i]<=w2[i] ? (w2[i]-w1[i]) : 1000000000);
    for(int i=0;i<n;++i) {
        if (d[1]) {
            mtree[1].first+=d[1];
            d[2]+=d[1]; d[3]+=d[1]; d[1]=0;
        }
        while(mtree[1].first<0) {
            update(1,0,n,mtree[1].second,1000000000);
        }
        ans[i]=mtree[1].first;
        --d[1];
        update(1,0,n,a2[i],n-1-w1[a2[i]]);
    }

    for(int i=0;i<500000;++i) mtree[i]=WW;
    memset(d,0,sizeof(d));
    for(int i=0;i<n;++i) update(1,0,n,i,w1[i]>=w2[i] ? (w1[i]-w2[i]) : 1000000000);
    int pos=0;
    for(int i=0;i<n;++i) {
        if (d[1]) {
            mtree[1].first+=d[1];
            d[2]+=d[1]; d[3]+=d[1]; d[1]=0;
        }
        while(mtree[1].first<0) {
            update(1,0,n,mtree[1].second,1000000000);
        }
        ans[pos]=min(ans[pos],mtree[1].first);
        --d[1];
        update(1,0,n,a2[n-1-i],w1[a2[n-1-i]]);
        pos--;
        if (pos<0) pos+=n;
    }
/*  cout << 100000 << endl;
    for(int i=0;i<100000;++i)
        cout << i+1 << "    ";
    cout << endl;
    for(int i=0;i<100000;++i)
        cout << 100000-i << "   ";
    cout << endl;*/
    for(int i=0;i<n;++i)
        printf("%d\n",ans[i]);
    return 0;
}


#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<vector>
#include<set>

using namespace std;

const int N = 100000 + 10;

multiset<int> st;

int ordA[N], b[N];

int main()
{
    int n, a;
    scanf("%d",&n);
    for(int i = 0; i < n; ++i)
    {
        scanf("%d",&a);
        ordA[a] = i;
    }
    for(int i = 0; i < n; ++i)
    {
        scanf("%d",&b[i]);
        st.insert(i - ordA[b[i]]);
    }
    multiset<int>::iterator it;
    for(int i = 0; i < n; ++i)
    {
        int ans = 1000000;
        it = st.lower_bound(i);
        if(it != st.end()) ans = min(ans, *it - i);
        if(it != st.begin()) ans = min(ans, i - *(--it));
        printf("%d\n",ans);
        st.erase(st.find(i - ordA[b[i]]));
        st.insert(n + i - ordA[b[i]]);
    }
    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#define _SECURE_SCL 0

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include <iostream>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <cctype>
#include <sstream>
#include <cassert>
#include <bitset>
#include <memory.h>

using namespace std;

#pragma comment(linker, "/STACK:200000000")

typedef long long int64;

#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define fore(i, a, n) for(int i = (int)(a); i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define last(a) (int(a.size()) - 1)
#define all(a) a.begin(), a.end()

const double EPS = 1E-9;
const int INF = 1000000000;
const int64 INF64 = (int64) 1E18;
const double PI = 3.1415926535897932384626433832795;

const int NMAX = 110000;

int a[NMAX], b[NMAX], ans[NMAX], x[NMAX], y[NMAX], v1[NMAX], v2[NMAX];

int main() {
#ifdef RADs_project
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif

  int n;
	cin >> n;
  forn(i, n)
    scanf("%d", &a[i]);
  forn(i, n)
    scanf("%d", &b[i]);

  if (n == 1) {
    puts("0");
    return 0;
  }

  forn(i, n) {
    a[i]--;
    b[i]--;
  }

  forn(i, n) {
    x[a[i]] = i;
    y[b[i]] = i;
  }

  set<pair<int, int> > q1, q2;
  ans[0] = INF;
  forn(i, n)
    if (x[i] <= y[i]) {
      v1[i] = y[i] - x[i];
      v2[i] = -1;
      q1.insert(mp(y[i] - x[i], i));
      ans[0] = min(ans[0], y[i] - x[i]);
    } else {
      v2[i] = x[i] - y[i];
      v1[i] = -1;
      q2.insert(mp(x[i] - y[i], i));
      ans[0] = min(ans[0], x[i] - y[i]);
    }

  int b1 = 0, b2 = 0;

  for (int i = 1; i < n; i++) {
    int id = b[i - 1];
    if (v1[id] == -1) {
      q2.erase(mp(v2[id], id));
    } else {
      q1.erase(mp(v1[id], id));
    }
    v1[id] = n - 1 - x[id] + i;
    v2[id] = -1;
    q1.insert(mp(v1[id], id));

    while (!q1.empty() && q1.begin()->fs < i) {
      int id = q1.begin()->sc;
      q1.erase(q1.begin());
      v1[id] = -1;
      v2[id] = 1 - i;
      q2.insert(mp(v2[id], id));
    }

    ans[i] = INF;
    if (!q1.empty()) {
      ans[i] = min(ans[i], q1.begin()->fs - i);
    }
    if (!q2.empty()) {
      ans[i] = min(ans[i], q2.begin()->fs + i);
    }
  }

  forn(i, n)
    printf("%d\n", ans[i]);

	return 0;
}


#pragma comment(linker,"/STACK:256000000")
#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <complex>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdlib>
#include <cstring>
using namespace std;
#define bublic public:
#define sz(v) ((int)(v).size())
#define forn(i,n) for(int i=0;i<(n);++i)
#define forv(i,v) forn(i,sz(v))
#define pb push_back
#define mp make_pair
#define istr stringstream
#define fi first
#define se second
#define X first
#define Y second
#define clr(a) memset((a),0,sizeof(a))
#define clr2(a) memset((a),-1,sizeof(a))
#define y0 asdy0
#define y1 asdy1
#define j0 asdj0
#define j1 asdj1
#define next asdnext
#define prev asdprev
#define link asdlink
#define unlink asdunlink
#define left asdleft
#define right asdright
#define hash asdhash
#define all(v) (v).begin(),(v).end()
typedef long long lng;
typedef unsigned int uint;
typedef unsigned long long ulng;
typedef unsigned char uchar;
typedef long double ld;
typedef pair<int,int> PII;
typedef pair<ld,ld> PDD;
typedef pair<lng,lng> PLL;
typedef complex<double> com;
const ld PI=3.1415926535897932384626433832795;
const ld EPS=1e-3;
const int inf=1000000000;
const lng linf=1000000000000000000LL;
template<class T> inline T sqr(T x) { return x * x; }

vector<pair<PII,PII> > evs[110000];
int A[110000];
int B[110000];
int res[110000];
int n;

int main() {
#ifdef __ASD__
    freopen("q.in","rt",stdin);freopen("q.out","wt",stdout);
#endif

	cin>>n;
	forn(i,n){
		int a;
		cin>>a;
		--a;
		A[a]=i;
	}
	forn(i,n){
		int a;
		cin>>a;
		--a;
		B[a]=i;
	}
	multiset<int> s[2]; // 0 - inc, 1 - dec
	forn(i,n){
		int a=A[i];
		int b=B[i];
		if(a>=b){
			s[1].insert(a-b);
			evs[a-b].pb(mp(mp(1,a-b),mp(0,-(a-b))));
			evs[n-1-b].pb(mp(mp(0,-(a-b)),mp(1,a+(n-b))));
		}else{
			s[0].insert(b-a);
			evs[n-1-b].pb(mp(mp(0,b-a),mp(1,a+(n-b))));
			evs[a-b+n].pb(mp(mp(1,a+(n-b)),mp(0,-(a-b+n))));
		}
	}

	forn(i,n){
		int &r=res[(n-i)%n];
		r=inf;
		if(s[0].size()){
			r=min(r,*s[0].begin()+i);
		}
		if(s[1].size()){
			r=min(r,*s[1].begin()-i);
		}
		forv(j,evs[i]){
			pair<PII,PII> p=evs[i][j];
			s[p.X.X].erase(s[p.X.X].lower_bound(p.X.Y));
			s[p.Y.X].insert(p.Y.Y);
		}
	}

	forn(i,n)
		cout<<res[i]<<'\n';

	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define maxn 101100
#define INF 1<<30
struct node{
    int id,val;
    node(){}
    node(int _val,int _id){ id=_id; val=_val; }
    friend bool operator <(node a,node b)
    {
        return a.val>b.val;
    }
};
priority_queue<node>A;
priority_queue<node>B;
int a[maxn],pos[maxn],b[maxn],n;
int un[maxn],ans[maxn];
void solve()
{
    int i,j;
    while(!A.empty()) A.pop();
    while(!B.empty()) B.pop();
    memset(un,0,sizeof(un));
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        pos[a[i]]=i;
    }
    for(i=0;i<n;i++)
    {
        scanf("%d",&b[i]);
        int tmp=i-pos[b[i]];
        if(tmp>0) A.push(node(tmp,i));
        else B.push(node(-tmp,i));
    }
    ans[0]=min(A.empty()?INF:A.top().val,B.empty()?INF:B.top().val);
    while(!B.empty()&&B.top().val==0)
    {
        int x=B.top().id;
        A.push(node(-1,x));
        B.pop();
    }
    int cur=1,len=1;
    for(i=n-1;i>=1;i--)
    {
        un[i]=1;
        int tmp=pos[b[i]];
        while(!A.empty()&&un[A.top().id])
        {
            un[A.top().id]=0;
            A.pop();
        }
        if(tmp==0) A.push(node(-cur,i));
        else B.push(node(cur+tmp,i));
        int an1=A.empty()?INF:A.top().val+cur;
        int an2=B.empty()?INF:B.top().val-cur;
        ans[len++]=an1<an2?an1:an2;
        if(an2==0)
        {
            while(!B.empty()&&B.top().val-cur==0)
            {
                int x=B.top().id;
                A.push(node(-(cur-1),x));
                B.pop();
            }
        }
        cur++;
    }
    printf("%d\n",ans[0]);
    for(i=len-1;i>0;i--)
        printf("%d\n",ans[i]);
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        solve();
    }
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define maxn 101100
#define INF 1<<30
struct node{
    int id,val;
    node(){}
    node(int _val,int _id){ id=_id; val=_val; }
    friend bool operator <(node a,node b)
    {
        return a.val>b.val;
    }
};
priority_queue<node>A;
priority_queue<node>B;
int a[maxn],pos[maxn],b[maxn],n;
int un[maxn],ans[maxn];
void solve()
{
    int i,j;
    while(!A.empty()) A.pop();
    while(!B.empty()) B.pop();
    memset(un,0,sizeof(un));
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        pos[a[i]]=i;
    }
    for(i=0;i<n;i++)
    {
        scanf("%d",&b[i]);
        int tmp=i-pos[b[i]];
        if(tmp>0) A.push(node(tmp,i));
        else B.push(node(-tmp,i));
    }
	ans[0]=min(A.empty()?INF:A.top().val,B.empty()?INF:B.top().val);
	while(!B.empty()&&B.top().val==0)
    {
        int x=B.top().id;
        A.push(node(0,x));
        B.pop();
    }
    int cur=1,len=1;
    for(i=n-1;i>=1;i--)
    {
		un[i]=1;
        int tmp=pos[b[i]];
        while(!A.empty()&&un[A.top().id])
        {
            un[A.top().id]=0;
            A.pop();
        }
        if(tmp==0) A.push(node(-cur,i));
        else B.push(node(cur+tmp,i));
        int an1=A.empty()?INF:A.top().val+cur;
        int an2=B.empty()?INF:B.top().val-cur;
        ans[len++]=an1<an2?an1:an2;
        if(an2==0)
        {
            while(!B.empty()&&B.top().val-cur==0)
            {
                int x=B.top().id;
                A.push(node(-cur,x));
                B.pop();
            }
        }
        cur++;
    }
    printf("%d\n",ans[0]);
    for(i=len-1;i>0;i--)
        printf("%d\n",ans[i]);
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        solve();
    }
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define maxn 101100
#define INF 1<<30
struct node{
    int id,val;
    node(){}
    node(int _val,int _id){ id=_id; val=_val; }
    friend bool operator <(node a,node b)
    {
        return a.val>b.val;
    }
};
priority_queue<node>A;
priority_queue<node>B;
int a[maxn],pos[maxn],b[maxn],n;
int un[maxn*2],ans[maxn];
void solve()
{
    int i,j;
    while(!A.empty()) A.pop();
    while(!B.empty()) B.pop();
    memset(un,0,sizeof(un));
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        pos[a[i]]=i;
    }
    for(i=0;i<n;i++)
    {
        scanf("%d",&b[i]);
        int tmp=i-pos[b[i]];
        if(tmp>0) A.push(node(tmp,i));
        else B.push(node(-tmp,i));
    }
    ans[0]=min(A.empty()?INF:A.top().val,B.empty()?INF:B.top().val);
    while(!B.empty()&&B.top().val==0)
    {
        int x=B.top().id;
        A.push(node(0,x));
        B.pop();
    }
    int cur=1,len=1;
    for(i=n-1;i>=1;i--)
    {
        un[i]=1;
        int tmp=pos[b[i]];
        while(!A.empty()&&un[A.top().id])
        {
            un[A.top().id]=0;
            A.pop();
        }
        if(tmp==0) A.push(node(-cur,i+n));
        else B.push(node(cur+tmp,i+n));
        int an1=A.empty()?INF:A.top().val+cur;
        int an2=B.empty()?INF:B.top().val-cur;
        ans[len++]=an1<an2?an1:an2;
        if(an2==0)
        {
            while(!B.empty()&&B.top().val-cur==0)
            {
                int x=B.top().id;
                A.push(node(-cur,x));
                B.pop();
            }
        }
        cur++;
    }
    printf("%d\n",ans[0]);
    for(i=len-1;i>0;i--)
        printf("%d\n",ans[i]);
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        solve();
    }
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define maxn 101100
#define INF 1<<30
struct node{
    int id,val;
    node(){}
    node(int _val,int _id){ id=_id; val=_val; }
    friend bool operator <(node a,node b)
    {
        return a.val>b.val;
    }
};
priority_queue<node>A;
priority_queue<node>B;
int a[maxn],pos[maxn],b[maxn],n;
int un[maxn],ans[maxn];
void solve()
{
    int i,j;
    while(!A.empty()) A.pop();
    while(!B.empty()) B.pop();
    memset(un,0,sizeof(un));
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        pos[a[i]]=i;
    }
    for(i=0;i<n;i++)
    {
        scanf("%d",&b[i]);
        int tmp=i-pos[b[i]];
        if(tmp>0) A.push(node(tmp,i));
        else B.push(node(-tmp,i));
    }
    ans[0]=min(A.empty()?INF:A.top().val,B.empty()?INF:B.top().val);
    int cur=1,len=1;
    for(i=n-1;i>=1;i--)
    {
        un[i]=1;
        int tmp=pos[b[i]];
        while(!A.empty()&&un[A.top().id])
        {
            un[A.top().id]=0;
            A.pop();
        }
        if(tmp==0) A.push(node(-cur,i));
        else B.push(node(cur+tmp,i));
        int an1=A.empty()?INF:A.top().val+cur;
        int an2=B.empty()?INF:B.top().val-cur;
        ans[len++]=an1<an2?an1:an2;
        if(an2==0)
        {
            while(!B.empty()&&B.top().val-cur==0)
            {
                int x=B.top().id;
                A.push(node(-cur,x));
                B.pop();
            }
        }
        cur++;
    }
    printf("%d\n",ans[0]);
    for(i=len-1;i>0;i--)
        printf("%d\n",ans[i]);
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        solve();
    }
}


#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <queue>
#include <set>

using namespace std;

vector<int>o,p,e;
int n,i,j,x;
int a[200000],b[200000];

struct T
{
    int i;
}t;

bool operator <(T t1,T t2)
{
    if((t2.i-x-a[b[t2.i]]+2*n)%n!=(t1.i-x-a[b[t1.i]]+2*n)%n)
        return (t2.i-x-a[b[t2.i]]+2*n)%n<(t1.i-x-a[b[t1.i]]+2*n)%n;
    return t1.i<t2.i;
}

priority_queue<T>qp;
set<T>qo;
set<T>::iterator it;

int main()
{
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&x);
        a[x-1]=i;
    }
    for(i=0;i<n;i++)
    {
        scanf("%d",&b[i]);
        b[i]--;
    }
    x=0;
    for(i=0;i<n;i++)
        if(a[b[i]]<i)
        {
            t.i=i;
            qp.push(t);
        }
        else if(a[b[i]]>i)
        {
            t.i=i;
            qo.insert(t);
        }
        else
            e.push_back(i);
    for(i=0;i<n;i++)
    {
        x=1999999999;
        if(e.size())
            printf("0\n");
        else
        {
            if(qp.size())
            {
                t=qp.top();
                x=min(x,(t.i-i+n)%n-a[b[t.i]]);
            }
            if(qo.size())
            {
                it=qo.begin();
                j=it->i;
                x=min(x,a[b[j]]-(j-i+n)%n);
            }
            printf("%d\n",x);
        }
        x=i+1;
        for(j=0;j<e.size();j++)
        {
            if(e[j]!=i)
            {
                t.i=e[j];
                qo.insert(t);
            }
            else
            {
                t.i=e[j];
                qp.push(t);
            }
        }
        e.clear();
        while(!qp.empty())
        {
            t=qp.top();
            if((t.i-i-1+n)%n==a[b[t.i]])
            {
                e.push_back(t.i);
                qp.pop();
            }
            else
                break;
        }
        t.i=i;
        it=qo.find(t);
        if(it!=qo.end())
        {
            qo.erase(t);
            if(a[b[i]]==n-1)
                e.push_back(i);
            else
            {
                t.i=i;
                qp.push(t);
            }
        }
    }
    return 0;
}


#include <iostream>
#include <set>
using namespace std;

int dif[100001];

struct cmp
{
	bool operator()(int a,int b)
	{
		if(dif[a]==dif[b])
			return a<b;
		return dif[a]<dif[b];
	}
};

set<int,cmp> S;

int B[100000];
int A[100000];
int loc[100000];
int ans[100000];

int main()
{
	int N;
	cin >> N;
	for(int i=0;i<N;i++)
	{
		cin >> A[i];
		A[i]--;
		loc[A[i]] = i;
	}
	for(int i=0;i<N;i++)
	{
		cin >> B[i];
		B[i]--;
		dif[i] = i - loc[B[i]];
	}
	int zero = 0;
	set<int,cmp>::iterator it;
	for(int i=0;i<N;i++)
		S.insert(i);
	for(int i=0;i<N;i++)
	{
		ans[i] = N;
		dif[N] = zero;
		it = S.lower_bound(N);
		if(it != S.end())
			ans[i] = min(ans[i],dif[*it] - zero);
		if(it != S.begin())
		{
			it--;
			ans[i] = min(ans[i],zero-dif[*it]);
		}
		zero++;
		S.erase(i);
		dif[i] += N;
		S.insert(i);
	}
	for(int i=0;i<N;i++)
		cout << ans[i] << "\n";
	return 0;
}


#include <iostream>
#include <set>
using namespace std;

int dif[100001];

struct cmp
{
	bool operator()(int a,int b)
	{
		if(dif[a]==dif[b])
			return a<b;
		return dif[a]<dif[b];
	}
};

set<int,cmp> S;

int B[100000];
int A[100000];
int loc[100000];
int ans[100000];

int main()
{
	int N;
	cin >> N;
	for(int i=0;i<N;i++)
	{
		cin >> B[i];
		B[i]--;
	}
	for(int i=0;i<N;i++)
	{
		cin >> A[i];
		A[i]--;
		loc[A[i]] = i;
	}
	for(int i=0;i<N;i++)
		dif[i] = i - loc[B[i]];
	int zero = 0;
	set<int,cmp>::iterator it;
	for(int i=0;i<N;i++)
		S.insert(i);
	for(int i=0;i<N;i++)
	{
		ans[i] = N;
		dif[N] = zero;
		it = S.lower_bound(N);
		if(it != S.end())
			ans[i] = min(ans[i],dif[*it] - zero);
		if(it != S.begin())
		{
			it--;
			ans[i] = min(ans[i],zero-dif[*it]);
		}
		zero--;
		S.erase(N-1-i);
		dif[N-1-i] -= N;
		S.insert(N-1-i);
	}
	for(int i=0;i<N;i++)
		cout << ans[i] << "\n";
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cassert>
#include <cstdlib>
#include <ctime>

using namespace std;
typedef long long int64;
#define E(c) cerr<<#c
#define Eo(x) cerr<<#x<<" = "<<(x)<<endl

const int SIZE = 100<<10;

int n;
int pa[SIZE], pb[SIZE];
int ra[SIZE], rb[SIZE];
int zerolvl;
int arr[SIZE];
map<int, int> vals;

int main() {
#ifdef _CRT_SECURE_NO_DEPRECATE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	scanf("%d", &n);
	for (int i = 0; i<n; i++) { scanf("%d", &pa[i]);  pa[i]--; }
	for (int i = 0; i<n; i++) { scanf("%d", &pb[i]);  pb[i]--; }
	for (int i = 0; i<n; i++) ra[pa[i]] = i;
	for (int i = 0; i<n; i++) rb[pb[i]] = i;

	for (int i = 0; i<n; i++) {
		arr[i] = rb[i] - ra[i];
		vals[arr[i]]++;
	}
	for (int i = 0; i<n; i++) {
//		for (int j = 0; j<n; j++) printf("%d ", arr[j] - zerolvl); printf("\n");
//		for (multiset<int>::iterator iter = vals.begin(); iter != vals.end(); iter++) printf("%d ", *iter); printf("\n");

		map<int, int>::iterator iter = vals.lower_bound(zerolvl);

		int q[2], qk = 0;
		if (iter != vals.end())
			q[qk++] = iter->first;
		if (iter != vals.begin()) {
			iter--;
			q[qk++] = iter->first;
		}

		int res = 100000000;
		for (int t = 0; t<qk; t++) {
			int d = q[t] - zerolvl;
			if (d < 0) d = -d;
			res = min(res, d);
		}
		printf("%d\n", res);

		zerolvl++;
		int mv = pb[i];

		vals[arr[mv]]--;
		if (vals[arr[mv]] == 0) vals.erase(arr[mv]);
			arr[mv] += n;
		vals[arr[mv]]++;
	}

	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>

#define MP make_pair
#define VI vector<int>
#define INF 1 << 31
#define PI 3.14159265358979
#define LL long long
#define PII pair<int,int>
using namespace std;

#pragma warning(disable:4996)

#define MAXN 100005

int N,A[MAXN],B[MAXN],P[MAXN];

multiset <int> S;
multiset <int>::iterator it;

int main()
{
#if _MSC_VER == 1500
	freopen("in.txt", "r", stdin);
#endif
	scanf("%d", &N);
	for(int i = 0; i < N; ++ i)
	{
		scanf("%d", A + i);
		P[A[i]] = i;
	}
	for(int i = 0; i < N; ++ i)
	{
		scanf("%d", B + i);
		S.insert(i - P[B[i]]);
	}

	//it = S.lower_bound(2);

	for(int i = 0; i < N; ++ i)
	{
		it = S.lower_bound(i);
		int ans = INF;
		if(it != S.end())
			ans = (*it - i);
		if(it != S.begin())
			ans = min(ans, abs(*(--it) - i));
		printf("%d\n", ans);
		S.erase(i - P[B[i]]);
		S.insert(i + N - P[B[i]]);
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>

#define MP make_pair
#define VI vector<int>
#define INF 1 << 31
#define PI 3.14159265358979
#define LL long long
#define PII pair<int,int>
using namespace std;

#pragma warning(disable:4996)

#define MAXN 100005

int N,A[MAXN],B[MAXN],P[MAXN];

multiset <int> S;
multiset <int>::iterator it;

int main()
{
#if _MSC_VER == 1500
	freopen("in.txt", "r", stdin);
#endif
	scanf("%d", &N);
	for(int i = 0; i < N; ++ i)
	{
		scanf("%d", A + i);
		P[A[i]] = i;
	}
	for(int i = 0; i < N; ++ i)
	{
		scanf("%d", B + i);
		S.insert(i - P[B[i]]);
	}

	//it = S.lower_bound(2);

	for(int i = 0; i < N; ++ i)
	{
		it = S.lower_bound(i);
		int ans = INF;
		if(it != S.end())
			ans = (*it - i);
		if(it != S.begin())
			ans = min(ans, abs(*(--it) - i));
		printf("%d\n", ans);
		it = S.find(i - P[B[i]]);
		S.erase(it);
		S.insert(i + N - P[B[i]]);
	}
	return 0;
}


#include <cstdio>
#include <set>

using namespace std;

#define N 100005

typedef multiset<int>::iterator MSIT;

int a[N],b[N],x[N],y[N];

int Min(int x,int y){
	return x<y?x:y;
}

int main(){
	int n,ans;
	scanf("%d",&n);
	for(int i=0;i<n;++i) scanf("%d",&a[i]);
	for(int i=0;i<n;++i) scanf("%d",&b[i]);
	for(int i=0;i<n;++i){ --a[i]; --b[i];}
	multiset<int> ss;
	for(int i=0;i<n;++i) x[a[i]]=y[b[i]]=i;
	for(int i=0;i<n;++i) ss.insert(x[i]-y[i]);
	for(int i=0;i<n;++i){
		ans=N;
		MSIT iter=ss.lower_bound(-i);
		if(iter!=ss.end()) ans=Min(ans,(*iter)+i);
		if(iter!=ss.begin()){
			--iter;
			ans=Min(ans,-(*iter)-i);
		}
		printf("%d\n",ans);
		int tt=x[b[i]]-y[b[i]];
		ss.erase(ss.find(tt));
		ss.insert(tt-n);
	}
	return 0;
}


#if 1
#define _CRT_SECURE_NO_DEPRECATE

#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <functional>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <ctime>
#include <cassert>
#include <sstream>
#include <iostream>
#include <bitset>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int , int> pii;
typedef vector <int> veci;
typedef vector <veci> graph;
const LD eps = 1e-9;
const LD pi = acos(-1.0);
const int inf = 1000 * 1000 * 1000;
const LL inf64 = LL(inf) * inf;

#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define iss istringstream
#define oss ostringstream
#define dbg(x) {cerr << #x << " = " << x << endl;}
#define dbgv(x) {cerr << #x << " ={"; for (int _i = 0; _i < x.size(); _i++) {if (_i) cerr << ", "; cerr << x[_i];} cerr << "}" << endl;}
#define NAME "problem"

int xabs(int a) { return a < 0 ? -a : +a; }

struct event_t
{
	int x;
	int num;
	int type; // 0 - shift a, 1 - shift b, 2 - query
	event_t() {}
	event_t(int x, int type, int num = -1) : x(x), type(type), num(num) {}

	friend bool operator < (const event_t &a, const event_t &b)
	{
		return mp(a.x, a.type) < mp(b.x, b.type);
	}
};

vector<int> brute(vector<int> a, vector<int> b)
{

	int n = a.size();
	vector<int> res(n);
	for(int i = 0; i < a.size(); ++i)
	{
		int minv = inf;

		for(int j = 0; j < n; ++j)
			for(int k = 0; k < n; ++k)
				if(a[j] == b[k])
					minv = min(minv, xabs(j - k));
		//cerr << "#" << minv << endl;
		res[i] = minv;
		int first = b[0];
		b.erase(b.begin());
		b.pb(first);
	}

	return res;
}

void gen(int n)
{
	freopen("input.txt", "w", stdout);
	vector<int> a(n), b(n);
	for(int i = 0; i < n; ++i)
		a[i] = b[i] = i + 1;
	random_shuffle(a.begin(), a.end());
	random_shuffle(b.begin(), b.end());
	cout << n << endl;
	for(int i = 0; i < n; ++i)
	{
		if(i) cout << " ";
		cout << a[i];
	}
	cout << endl;
	for(int i = 0; i < n; ++i)
	{
		if(i) cout << " ";
		cout << b[i];
	}
	cout << endl;
	//exit(0);
	freopen("con", "w", stdout);
}

void solve()
{
	//gen(76);
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	//freopen(NAME ".in","r",stdin);freopen(NAME ".out","w",stdout);

	int n;
	scanf("%d", &n);
	vector< int> a(n);
	for(int i = 0; i < n; ++i)
	{
		scanf("%d", &a[i]);
		a[i]--;
	}

	vector<int> b(n);
	for(int i = 0; i < n; ++i)
	{
		scanf("%d", &b[i]);
		b[i]--;
	}

	vector<int> pos_a(n), pos_b(n);
	for(int i = 0; i < n; ++i)
	{
		pos_a[a[i]] = i;
		pos_b[b[i]] = i;
	}

	vector<event_t> events;
	for(int i = 0; i < n; ++i)
		events.pb(event_t(i, 2));
	for(int i = 0; i < n; ++i)
	{
		//events.pb(event_t(i + 1, 0, a[i]));
		events.pb(event_t(i + 1, 0, b[i]));
		if(pos_a[b[i]] < i)
			events.pb(event_t(i - pos_a[b[i]], 0, b[i]));
		else
			events.pb(event_t(i + n - pos_a[b[i]], 0, b[i]));
	}

	sort(events.begin(), events.end());

	vector<int> res(n);
	multiset<int> dif;

	vector<int> last(n);
	vector<int> prev_b(n);
	multiset<int> dif_low, dif_up;
	for(int i = 0; i < n; ++i)
	{
		prev_b[i] = pos_b[i];
		last[i] = xabs(pos_a[i] - pos_b[i]);
		if(pos_a[i] < pos_b[i])
			dif_low.insert(last[i]);
		else
			dif_up.insert(last[i]);

	}
	int zero_low = 0, zero_up = 0;
	for(int i = 0; i < events.size(); ++i)
	{
		event_t e = events[i];
		if(e.type == 0)
		{
			int pos1o = pos_a[e.num];
			int pos2o = prev_b[e.num];

			if(pos1o < pos2o)
				dif_low.erase(dif_low.find(last[e.num]));
			else
				dif_up.erase(dif_up.find(last[e.num]));

			int pos1 = (pos_a[e.num]) % n;
			int pos2 = (pos_b[e.num] - e.x + n) % n;
			prev_b[e.num] = pos2;
			int diff = xabs(pos1 - pos2);

			if(pos1 < pos2)
			{
				last[e.num] = diff - zero_low;
				dif_low.insert(last[e.num]);
			}
			else
			{
				last[e.num] = diff - zero_up;
				dif_up.insert(last[e.num]);
			}

		} else if(e.type == 1)
		{
			assert(false);
		} else if(e.type == 2)
		{

			res[e.x] = inf;
			if(dif_low.size())
				res[e.x] = min(res[e.x], *dif_low.begin() + zero_low);
			if(dif_up.size())
				res[e.x] = min(res[e.x], *dif_up.begin() + zero_up);
			zero_low--;
			zero_up++;
		}
	}

	for(int i = 0; i < res.size(); ++i)
		printf("%d\n", res[i]);

	//vector<int> cres = brute(a, b);
	//for(int i = 0; i < res.size(); ++i)
	//	if(cres[i] != res[i])
	//	{
	//		cerr << "BAD at #" << i << endl;
	//		exit(0);
	//	}
	//cerr << "OK" << endl;

}
int main()
{
	//for(int it = 0; it < 1000; ++it)
	//{
	//	cerr << it << ": ";
	//	srand(it *34);
		solve();
	//}
	return 0;
}
/*************************
*************************/
#endif


#include <stdio.h>
#include <algorithm>
#include <set>

using namespace std;

#define NMAX 100100

int pa[NMAX], pb[NMAX];
int m1[NMAX], m2[NMAX];
int extmax1[NMAX], extmax2[NMAX];
set<pair<int, int> > final_seg;
int i, j, N, x;

int fastSolution() {
	// Stage 1.
	for (i = 0; i < N; i++) {
		m1[i] = N;
		extmax1[i] = 0;
	}

	for (i = 1; i <= N; i++)
		if (pb[i] >= pa[i]) {
			int itzero = pb[i] - pa[i];
			m1[itzero] = 0;
			extmax1[itzero] = max(extmax1[itzero], itzero);

			int itcomeback = pb[i];
			if (itcomeback < N) {
				int vmin = pb[i] - pa[i] + 1;
				final_seg.insert(make_pair(vmin, itcomeback));
			}
		} else {
			int itzero = pb[i] + N - pa[i];
			m1[itzero] = 0;
			extmax1[itzero] = max(extmax1[itzero], N - pa[i]);
		}

	i = N - 1;
	while (i >= 0) {
		if (m1[i] == 0) {
			if (i - extmax1[i] < 0)
				return 1;
			for (j = 1; j <= extmax1[i]; j++) {
				if (m1[i - j] == 0) {
					final_seg.insert(make_pair(i - (N - 1), i - extmax1[i]));
					break;
				}
				m1[i - j] = min(m1[i - j], j);
			}
			i -= j;
		} else {
			while (1) {
				if (final_seg.size() == 0)
					break;
				pair<int, int> p = *(final_seg.begin());
				if (p.second > i) {
					//fprintf(stderr, "S1-size1=%d\n", final_seg.size());
					final_seg.erase(p);
					//fprintf(stderr, "S1-size2=%d\n", final_seg.size());
				} else {
					m1[i] = min(m1[i], p.first + N - 1 - i);
					break;
				}
			}
			i--;
		}
	}

	//for (i = 0; i <= N - 1; i++)
	//	fprintf(stderr, "i=%d: m1[i]=%d\n", i, m1[i]);

	// Stage 2.
	for (i = 0; i < N; i++) {
		m2[i] = N;
		extmax2[i] = 0;
	}

	final_seg.clear();
	for (i = 1; i <= N; i++)
		if (pb[i] < pa[i]) {
			int itzero = pb[i] + N - pa[i];
			m2[itzero] = 0;
			extmax2[itzero] = max(extmax2[itzero], N - 1 - itzero);

			int itend = pb[i] - 1;
			final_seg.insert(make_pair(pa[i] - pb[i], itend));
		} else {
			int itzero = pb[i] - pa[i];
			m2[itzero] = 0;
			extmax2[itzero] = max(extmax2[itzero], pa[i] - 1);
		}

	i = 0;
	while (i < N) {
		if (m2[i] == 0) {
			if (i + extmax2[i] >= N)
				return 1;
			for (j = 1; j <= extmax2[i]; j++) {
				if (m2[i + j] == 0) {
					final_seg.insert(make_pair(-i, i + extmax2[i]));
					break;
				}
				m2[i + j] = min(m2[i + j], j);
			}
			i += j;
		} else {
			while (1) {
				if (final_seg.size() == 0)
					break;
				pair<int, int> p = *(final_seg.begin());
				if (p.second < i) {
					//fprintf(stderr, "S2-size1=%d\n", final_seg.size());
					final_seg.erase(p);
					//fprintf(stderr, "S2-size2=%d\n", final_seg.size());
				} else {
					m2[i] = min(m2[i], p.first + i);
					break;
				}
			}
			i++;
		}
	}

	//for (i = 0; i <= N - 1; i++)
	//	fprintf(stderr, "i=%d: m2[i]=%d\n", i, m2[i]);

	//for (i = 0; i <= N - 1; i++)
	//	printf("i=%d, m1[i]=%d, m2[i]=%d, ans=%d\n", i, m1[i], m2[i], min(m1[i], m2[i]));

	for (i = 0; i < N; i++)
		printf("%d\n", min(m1[i], m2[i]));

	return 0;
}

int slowSolution() {
	for (i = 0; i < N; i++) {
		int dmin = N, minPair = -1;
		for (j = 1; j <= N; j++) {
			int d = abs(pa[j] - pb[j]);
			if (d < dmin) {
				dmin = d;
				minPair = j;
			}

			pb[j]--;
			if (pb[j] == 0)
				pb[j] = N;
		}
		printf("%d\n", dmin);
	}
	return 0;
}

int main() {
	//freopen("xx.in", "r", stdin);

	scanf("%d", &N);
	for (i = 1; i <= N; i++) {
		scanf("%d", &x);
		pa[x] = i;
	}

	for (i = 1; i <= N; i++) {
		scanf("%d", &x);
		pb[x] = i;
	}

	//freopen("xs.out", "w", stdout);
	return fastSolution();
}


#include <stdio.h>
#include <algorithm>
#include <set>

using namespace std;

#define NMAX 100100

int pa[NMAX], pb[NMAX];
int m1[NMAX], m2[NMAX];
int extmax1[NMAX], extmax2[NMAX];
set<pair<int, int> > final_seg;
int i, j, N, x;

int fastSolution() {
	// Stage 1.
	for (i = 0; i < N; i++) {
		m1[i] = N;
		extmax1[i] = 0;
	}

	for (i = 1; i <= N; i++)
		if (pb[i] >= pa[i]) {
			int itzero = pb[i] - pa[i];
			m1[itzero] = 0;
			extmax1[itzero] = max(extmax1[itzero], itzero);

			int itcomeback = pb[i];
			if (itcomeback < N) {
				int vmin = pb[i] - pa[i] + 1;
				final_seg.insert(make_pair(vmin, itcomeback));
			}
		} else {
			int itzero = pb[i] + N - pa[i];
			m1[itzero] = 0;
			extmax1[itzero] = max(extmax1[itzero], N - pa[i]);
		}

	i = N - 1;
	while (i >= 0) {
		if (m1[i] == 0) {
			if (i - extmax1[i] < 0)
				return 1;
			for (j = 1; j <= extmax1[i]; j++) {
				if (m1[i - j] == 0)
					break;
				m1[i - j] = min(m1[i - j], j);
			}
			i -= j;
		} else {
			while (1) {
				if (final_seg.size() == 0)
					break;
				pair<int, int> p = *(final_seg.begin());
				if (p.second > i) {
					fprintf(stderr, "S1-size1=%d\n", final_seg.size());
					final_seg.erase(p);
					fprintf(stderr, "S1-size2=%d\n", final_seg.size());
				} else {
					m1[i] = min(m1[i], p.first + N - 1 - i);
					break;
				}
			}
			i--;
		}
	}

	//for (i = 0; i <= N - 1; i++)
	//	fprintf(stderr, "i=%d: m1[i]=%d\n", i, m1[i]);

	// Stage 2.
	for (i = 0; i < N; i++) {
		m2[i] = N;
		extmax2[i] = 0;
	}

	final_seg.clear();
	for (i = 1; i <= N; i++)
		if (pb[i] < pa[i]) {
			int itzero = pb[i] + N - pa[i];
			m2[itzero] = 0;
			extmax2[itzero] = max(extmax2[itzero], N - 1 - itzero);

			int itend = pb[i] - 1;
			final_seg.insert(make_pair(pa[i] - pb[i], itend));
		} else {
			int itzero = pb[i] - pa[i];
			m2[itzero] = 0;
			extmax2[itzero] = max(extmax2[itzero], pa[i] - 1);
		}

	i = 0;
	while (i < N) {
		if (m2[i] == 0) {
			if (i + extmax2[i] >= N)
				return 1;
			for (j = 1; j <= extmax2[i]; j++) {
				if (m2[i + j] == 0)
					break;
				m2[i + j] = min(m2[i + j], j);
			}
			i += j;
		} else {
			while (1) {
				if (final_seg.size() == 0)
					break;
				pair<int, int> p = *(final_seg.begin());
				if (p.second < i) {
					fprintf(stderr, "S2-size1=%d\n", final_seg.size());
					final_seg.erase(p);
					fprintf(stderr, "S2-size2=%d\n", final_seg.size());
				} else {
					m2[i] = min(m2[i], p.first + i);
					break;
				}
			}
			i++;
		}
	}

	//for (i = 0; i <= N - 1; i++)
	//	fprintf(stderr, "i=%d: m2[i]=%d\n", i, m2[i]);

	for (i = 0; i <= N - 1; i++)
		printf("%d\n", min(m1[i], m2[i]));

	return 0;
}

int slowSolution() {
	for (i = 0; i < N; i++) {
		int dmin = N;
		for (j = 1; j <= N; j++) {
			dmin = min(dmin, abs(pa[j] - pb[j]));
			pb[j]--;
			if (pb[j] == 0)
				pb[j] = N;
		}
		printf("%d\n", dmin);
	}
	return 0;
}

int main() {
	//freopen("x.in", "r", stdin);

	scanf("%d", &N);
	for (i = 1; i <= N; i++) {
		scanf("%d", &x);
		pa[x] = i;
	}

	for (i = 1; i <= N; i++) {
		scanf("%d", &x);
		pb[x] = i;
	}

	//freopen("xf.out", "w", stdout);
	return fastSolution();
}


#include <stdio.h>
#include <algorithm>
#include <set>

using namespace std;

#define NMAX 100100

int pa[NMAX], pb[NMAX];
int m1[NMAX], m2[NMAX];
int extmax1[NMAX], extmax2[NMAX];
set<pair<int, int> > final_seg;
int i, j, N, x;

int fastSolution() {
	// Stage 1.
	for (i = 0; i < N; i++) {
		m1[i] = N;
		extmax1[i] = 0;
	}

	for (i = 1; i <= N; i++)
		if (pb[i] >= pa[i]) {
			int itzero = pb[i] - pa[i];
			m1[itzero] = 0;
			extmax1[itzero] = max(extmax1[itzero], itzero);

			int itcomeback = pb[i];
			if (itcomeback < N) {
				int vmin = pb[i] - pa[i] + 1;
				final_seg.insert(make_pair(vmin, itcomeback));
			}
		} else {
			int itzero = pb[i] + N - pa[i];
			m1[itzero] = 0;
			extmax1[itzero] = max(extmax1[itzero], N - pa[i]);
		}

	i = N - 1;
	while (i >= 0) {
		if (m1[i] == 0) {
			if (i - extmax1[i] < 0)
				return 1;
			for (j = 1; j <= extmax1[i]; j++) {
				if (m1[i - j] == 0) {
					final_seg.insert(make_pair(i - (N - 1), i - extmax1[i]));
					break;
				}
				m1[i - j] = min(m1[i - j], j);
			}
			i -= j;
		} else {
			while (1) {
				if (final_seg.size() == 0)
					break;
				pair<int, int> p = *(final_seg.begin());
				if (p.second > i) {
					//fprintf(stderr, "S1-size1=%d\n", final_seg.size());
					final_seg.erase(p);
					//fprintf(stderr, "S1-size2=%d\n", final_seg.size());
				} else {
					m1[i] = min(m1[i], p.first + N - 1 - i);
					break;
				}
			}
			i--;
		}
	}

	//for (i = 0; i <= N - 1; i++)
	//	fprintf(stderr, "i=%d: m1[i]=%d\n", i, m1[i]);

	// Stage 2.
	for (i = 0; i < N; i++) {
		m2[i] = N;
		extmax2[i] = 0;
	}

	final_seg.clear();
	for (i = 1; i <= N; i++)
		if (pb[i] < pa[i]) {
			int itzero = pb[i] + N - pa[i];
			m2[itzero] = 0;
			extmax2[itzero] = max(extmax2[itzero], N - 1 - itzero);

			int itend = pb[i] - 1;
			final_seg.insert(make_pair(pa[i] - pb[i], itend));
		} else {
			int itzero = pb[i] - pa[i];
			m2[itzero] = 0;
			extmax2[itzero] = max(extmax2[itzero], pa[i] - 1);
		}

	i = 0;
	while (i < N) {
		if (m2[i] == 0) {
			if (i + extmax2[i] >= N)
				return 1;
			for (j = 1; j <= extmax2[i]; j++) {
				if (m2[i + j] == 0) {
					final_seg.insert(make_pair(-i, i + extmax2[i]));
					break;
				}
				m2[i + j] = min(m2[i + j], j);
			}
			i += j;
		} else {
			while (1) {
				if (final_seg.size() == 0)
					break;
				pair<int, int> p = *(final_seg.begin());
				if (p.second < i) {
					//fprintf(stderr, "S2-size1=%d\n", final_seg.size());
					final_seg.erase(p);
					//fprintf(stderr, "S2-size2=%d\n", final_seg.size());
				} else {
					m2[i] = min(m2[i], p.first + i);
					break;
				}
			}
			i++;
		}
	}

	//for (i = 0; i <= N - 1; i++)
	//	fprintf(stderr, "i=%d: m2[i]=%d\n", i, m2[i]);

	//for (i = 0; i <= N - 1; i++)
	//	printf("i=%d, m1[i]=%d, m2[i]=%d, ans=%d\n", i, m1[i], m2[i], min(m1[i], m2[i]));

	for (i = 0; i < N; i++)
		printf("%d\n", min(m1[i], m2[i]));

	return 0;
}

int slowSolution() {
	for (i = 0; i < N; i++) {
		int dmin = N, minPair = -1;
		for (j = 1; j <= N; j++) {
			int d = abs(pa[j] - pb[j]);
			if (d < dmin) {
				dmin = d;
				minPair = j;
			}

			pb[j]--;
			if (pb[j] == 0)
				pb[j] = N;
		}
		printf("%d\n", dmin);
	}
	return 0;
}

int main() {
	//freopen("xx.in", "r", stdin);

	scanf("%d", &N);
	for (i = 1; i <= N; i++) {
		scanf("%d", &x);
		pa[x] = i;
	}

	for (i = 1; i <= N; i++) {
		scanf("%d", &x);
		pb[x] = i;
	}

	//freopen("xs.out", "w", stdout);
	return slowSolution();
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#define N 100001

using namespace std;

int n, minDist;
int a[N], b[N];
map< int, int > distInit;
set< pair<int, int> > pos, neg;
pair<int, int> tmpPair;

int main()
{
    cin >> n;
    if(!n)  return 0;
    for(int i=0; i < n; ++i)
    {
        cin >> a[i];
    }
    for(int i=0; i < n; ++i)
    {
        cin >> b[i];
    }

    for(int i=0; i < n; ++i)
    {
        distInit[ b[i] ] = i;
    }
    for(int i=0; i < n; ++i)
    {
        distInit[ a[i] ] -= i;
    }

    for(int i=0; i < n; ++i)
    {
        tmpPair = make_pair(distInit[ a[i] ], a[i]);
        if(tmpPair.first > 0)
        {
            pos.insert(tmpPair);
        }
        else
        {
            neg.insert(tmpPair);
        }
    }

    set< pair<int, int> >::iterator it1=pos.begin(), it2 = neg.end();

    minDist = INT_MAX;  //one of the sets have to be non-empty, thus we'll never print this val
    it1 = pos.begin();
    if(it1 != pos.end())
        minDist = it1->first;

    if(it2 != neg.begin())
    {
        it2--;
        minDist = min(minDist, abs(it2->first));
    }
    cout << minDist << endl;

    for(int i=1; i < n; ++i)
    {
        it1 = pos.begin();
        while(it1 != pos.end() && (it1->first - i) <= 0)
        {
            //take the now-positive element to the other group(pos)
            tmpPair = *it1;
            pos.erase(it1);
            neg.insert(tmpPair);

            it1 = pos.begin();  // just incrementing the iterator may result in incorrect update of the next element in the Red-Black tree
        }

        tmpPair = make_pair(distInit[b[ i-1 ]], b[ i-1 ]);
        it2 = neg.find(tmpPair);
        if(it2 != neg.end())
        {
            neg.erase(it2);
            tmpPair.first += (n);
            pos.insert(tmpPair);
        }

        minDist = INT_MAX;  //one of the sets have to be non-empty, thus we'll never print this val
        it2 = neg.end();
        if(it2 != neg.begin())
        {
            it2--;
            minDist = abs(it2->first - i);
        }

        it2 = pos.begin();
        if(it2 != neg.end())
        {
            minDist = min(minDist, it2->first - i);
            /*
            it1 = pos.begin();
            while(it1 != it2 && it1->first - i >= 0)
            {
                minDist = min(minDist, abs(it1->first - i));
                it1--;
            }
            */
        }
        cout << minDist << endl;
    }

    return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#define N 100001

using namespace std;

int n, minDist;
int a[N], b[N];
int distInit[N];
set< pair<int, int> > pos, neg;
pair<int, int> tmpPair;

int main()
{
    cin >> n;
    if(!n)  return 0;
    for(int i=0; i < n; ++i)
    {
        cin >> a[i];
    }
    for(int i=0; i < n; ++i)
    {
        cin >> b[i];
    }

    for(int i=0; i < n; ++i)
    {
        distInit[ b[i] ] = i;
    }
    for(int i=0; i < n; ++i)
    {
        distInit[ a[i] ] -= i;
    }

    for(int i=0; i < n; ++i)
    {
        tmpPair = make_pair(distInit[ a[i] ], a[i]);
        if(tmpPair.first > 0)
        {
            pos.insert(tmpPair);
        }
        else
        {
            neg.insert(tmpPair);
        }
    }

    set< pair<int, int> >::iterator it1=pos.begin(), it2 = neg.end();

    minDist = INT_MAX;  //one of the sets have to be non-empty, thus we'll never print this val
    it1 = pos.begin();
    if(it1 != pos.end())
        minDist = it1->first;

    if(it2 != neg.begin())
    {
        it2--;
        minDist = min(minDist, abs(it2->first));
    }
    cout << minDist << endl;

    for(int i=1; i < n; ++i)
    {
        it1 = pos.begin();
        while(it1 != pos.end() && (it1->first - i) <= 0)
        {
            //take the now-positive element to the other group(pos)
            tmpPair = *it1;
            pos.erase(it1);
            neg.insert(tmpPair);

            it1 = pos.begin();  // just incrementing the iterator may result in incorrect update of the next element in the Red-Black tree
        }

        tmpPair = make_pair(distInit[b[ i-1 ]], b[ i-1 ]);
        it2 = neg.find(tmpPair);
        if(it2 != neg.end())
        {
            neg.erase(it2);
            tmpPair.first += (n);
            pos.insert(tmpPair);
        }

        minDist = INT_MAX;  //one of the sets have to be non-empty, thus we'll never print this val
        it2 = neg.end();
        if(it2 != neg.begin())
        {
            it2--;
            minDist = abs(it2->first - i);
        }

        it2 = pos.begin();
        if(it2 != pos.end())
        {
            minDist = min(minDist, it2->first - i);
            /*
            it1 = pos.begin();
            while(it1 != it2 && it1->first - i >= 0)
            {
                minDist = min(minDist, abs(it1->first - i));
                it1--;
            }
            */
        }
        cout << minDist << endl;
    }

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <queue>
#include <set>

using namespace std;

vector<int>o,p,e;
int n,i,j,x;
int a[200000],b[200000];

struct T
{
    int i;
}t;

bool operator <(T i,T j)
{
    return (i.i-a[b[i.i]]+n)%n<(j.i-a[b[j.i]]+n)%n;
}

bool pr2(int j,int i)
{
    return (i-a[b[i]]+n)%n<(j-a[b[j]]+n)%n;
}

priority_queue<T>qp;
queue<int>qo;

int main()
{
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&x);
        a[x-1]=i;
    }
    for(i=0;i<n;i++)
    {
        scanf("%d",&b[i]);
        b[i]--;
    }
    for(i=0;i<n;i++)
        if(a[b[i]]<i)
        {
            t.i=i;
            qp.push(t);
        }
        else if(a[b[i]]>i)
            o.push_back(i);

        else
            e.push_back(i);
    sort(o.begin(),o.end(),pr2);
    for(i=0;i<o.size();i++)
        qo.push(o[i]);
    for(i=0;i<n;i++)
    {
        x=1999999999;
        if(e.size())
            printf("0\n");
        else
        {
            if(qp.size())
            {
                t=qp.top();
                x=min(x,(t.i-i+n)%n-a[b[t.i]]);
            }
            if(qo.size())
            {
                j=qo.back();
                x=min(x,a[b[j]]-(j-i+n)%n);
            }
            printf("%d\n",x);
        }
        x=1999999999;
        for(j=0;j<e.size();j++)
        {
            if(e[j]!=i)
                qo.push(e[j]);
            else
            {
                t.i=e[j];
                qp.push(t);
            }
        }
        e.clear();
        while(!qp.empty())
        {
            t=qp.top();
            if((t.i-i-1+n)%n==a[b[t.i]])
            {
                e.push_back(t.i);
                qp.pop();
            }
            else
                break;
        }
        if(!qo.empty())
        {
            j=qo.front();
            if(j==i)
            {
                qo.pop();
                if(a[b[j]]==n-1)
                    e.push_back(j);
                else
                {
                    t.i=j;
                    qp.push(t);
                }
            }
        }
    }
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <queue>
#include <set>

using namespace std;

vector<int>o,p,e;
int n,i,j,x;
int a[200000],b[200000];

struct T
{
    int i;
}t;

bool operator <(T t1,T t2)
{
    if((t2.i-x-a[b[t2.i]]+2*n)%n!=(t1.i-x-a[b[t1.i]]+2*n)%n)
        return (t2.i-x-a[b[t2.i]]+2*n)%n<(t1.i-x-a[b[t1.i]]+2*n)%n;
    return t1.i<t2.i;
}

priority_queue<T>qp;
set<T>qo;
set<T>::iterator it;

int main()
{
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&x);
        a[x-1]=i;
    }
    for(i=0;i<n;i++)
    {
        scanf("%d",&b[i]);
        b[i]--;
    }
    x=0;
    for(i=0;i<n;i++)
        if(a[b[i]]<i)
        {
            t.i=i;
            qp.push(t);
        }
        else if(a[b[i]]>i)
        {
            t.i=i;
            qo.insert(t);
        }
        else
            e.push_back(i);
    for(i=0;i<n;i++)
    {
        x=1999999999;
        if(e.size())
            printf("0\n");
        else
        {
            if(qp.size())
            {
                t=qp.top();
                x=min(x,(t.i-i+n)%n-a[b[t.i]]);
            }
            if(qo.size())
            {
                it=qo.end();
                --it;
                j=it->i;
                x=min(x,a[b[j]]-(j-i+n)%n);
            }
            printf("%d\n",x);
        }
        x=i+1;
        for(j=0;j<e.size();j++)
        {
            if(e[j]!=i)
            {
                t.i=e[j];
                qo.insert(t);
            }
            else
            {
                t.i=e[j];
                qp.push(t);
            }
        }
        e.clear();
        while(!qp.empty())
        {
            t=qp.top();
            if((t.i-i-1+n)%n==a[b[t.i]])
            {
                e.push_back(t.i);
                qp.pop();
            }
            else
                break;
        }
        t.i=i;
        it=qo.find(t);
        if(it!=qo.end())
        {
            qo.erase(t);
            if(a[b[i]]==n-1)
                e.push_back(i);
            else
            {
                t.i=i;
                qp.push(t);
            }
        }
    }
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <queue>
#include <set>

using namespace std;

vector<int>o,p,e;
int n,i,j,x;
int a[200000],b[200000];

struct T
{
    int i;
}t;

bool operator <(T t1,T t2)
{
    return (t2.i-x-a[b[t2.i]]+2*n)%n<(t1.i-x-a[b[t1.i]]+2*n)%n;
}

bool pr2(int i,int j)
{
    return (a[b[i]]-i)>(a[b[j]]-j);
}

priority_queue<T>qp;
queue<int>qo;

int main()
{
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&x);
        a[x-1]=i;
    }
    for(i=0;i<n;i++)
    {
        scanf("%d",&b[i]);
        b[i]--;
    }
    x=0;
    for(i=0;i<n;i++)
        if(a[b[i]]<i)
        {
            t.i=i;
            qp.push(t);
        }
        else if(a[b[i]]>i)
            o.push_back(i);
        else
            e.push_back(i);
    sort(o.begin(),o.end(),pr2);
    for(i=0;i<o.size();i++)
        qo.push(o[i]);
    for(i=0;i<n;i++)
    {
        x=1999999999;
        if(e.size())
            printf("0\n");
        else
        {
            if(qp.size())
            {
                t=qp.top();
                x=min(x,(t.i-i+n)%n-a[b[t.i]]);
            }
            if(qo.size())
            {
                j=qo.back();
                x=min(x,a[b[j]]-(j-i+n)%n);
            }
            printf("%d\n",x);
        }
        x=i+1;
        for(j=0;j<e.size();j++)
        {
            if(e[j]!=i)
                qo.push(e[j]);
            else
            {
                t.i=e[j];
                qp.push(t);
            }
        }
        e.clear();
        while(!qp.empty())
        {
            t=qp.top();
            if((t.i-i-1+n)%n==a[b[t.i]])
            {
                e.push_back(t.i);
                qp.pop();
            }
            else
                break;
        }
        if(!qo.empty())
        {
            j=qo.front();
            if(j==i)
            {
                qo.pop();
                if(a[b[j]]==n-1)
                    e.push_back(j);
                else
                {
                    t.i=j;
                    qp.push(t);
                }
            }
        }
    }
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <queue>
#include <set>

using namespace std;

vector<int>o,p,e;
int n,i,j,x;
int a[200000],b[200000];

struct T
{
    int i;
}t;

bool operator <(T i,T j)
{
    return (i.i-a[b[i.i]]+n)%n<(j.i-a[b[j.i]]+n)%n;
}

bool pr2(int i,int j)
{
    return (a[b[i]]-i)>(a[b[j]]-j);
}

priority_queue<T>qp;
queue<int>qo;

int main()
{
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&x);
        a[x-1]=i;
    }
    for(i=0;i<n;i++)
    {
        scanf("%d",&b[i]);
        b[i]--;
    }
    for(i=0;i<n;i++)
        if(a[b[i]]<i)
        {
            t.i=i;
            qp.push(t);
        }
        else if(a[b[i]]>i)
            o.push_back(i);
        else
            e.push_back(i);
    sort(o.begin(),o.end(),pr2);
    for(i=0;i<o.size();i++)
        qo.push(o[i]);
    for(i=0;i<n;i++)
    {
        x=1999999999;
        if(e.size())
            printf("0\n");
        else
        {
            if(qp.size())
            {
                t=qp.top();
                x=min(x,(t.i-i+n)%n-a[b[t.i]]);
            }
            if(qo.size())
            {
                j=qo.back();
                x=min(x,a[b[j]]-(j-i+n)%n);
            }
            printf("%d\n",x);
        }
        x=1999999999;
        for(j=0;j<e.size();j++)
        {
            if(e[j]!=i)
                qo.push(e[j]);
            else
            {
                t.i=e[j];
                qp.push(t);
            }
        }
        e.clear();
        while(!qp.empty())
        {
            t=qp.top();
            if((t.i-i-1+n)%n==a[b[t.i]])
            {
                e.push_back(t.i);
                qp.pop();
            }
            else
                break;
        }
        if(!qo.empty())
        {
            j=qo.front();
            if(j==i)
            {
                qo.pop();
                if(a[b[j]]==n-1)
                    e.push_back(j);
                else
                {
                    t.i=j;
                    qp.push(t);
                }
            }
        }
    }
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <queue>
#include <set>

using namespace std;

vector<int>o,p,e;
int n,i,j,x;
int a[200000],b[200000];
queue<int>qp,qo;

bool pr(int i,int j)
{
    return i-a[b[i]]<j-a[b[j]];
}

bool pr2(int i,int j)
{
    return pr(j,i);
}

int main()
{
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&x);
        a[x-1]=i;
    }
    for(i=0;i<n;i++)
    {
        scanf("%d",&b[i]);
        b[i]--;
    }
    for(i=0;i<n;i++)
    {
        if(a[b[i]]<i)
            p.push_back(i);
        else if(a[b[i]]>i)
            o.push_back(i);
        else
            e.push_back(i);
    }
    sort(p.begin(),p.end(),pr);
    sort(o.begin(),o.end(),pr2);
    for(i=0;i<p.size();i++)
        qp.push(p[i]);
    for(i=0;i<o.size();i++)
        qo.push(o[i]);
    for(i=0;i<n;i++)
    {
        x=1999999999;
        if(e.size())
            printf("0\n");
        else
        {
            if(qp.size())
            {
                j=qp.front();
                x=min(x,(j-i+n)%n-a[b[j]]);
            }
            if(qo.size())
            {
                j=qo.back();
                x=min(x,a[b[j]]-(j-i+n)%n);
            }
            printf("%d\n",x);
        }
        for(j=0;j<e.size();j++)
        {
            if(e[j]!=i)
                qo.push(e[j]);
            else
                qp.push(e[j]);
        }
        e.clear();
        while(!qp.empty())
        {
            j=qp.front();
            if((j-i-1+n)%n==a[b[j]])
            {
                e.push_back(j);
                qp.pop();
            }
            else
                break;
        }
        if(!qo.empty())
        {
            j=qo.front();
            if(j==i)
            {
                qo.pop();
                if(a[b[j]]==n-1)
                    e.push_back(j);
                else
                    qp.push(j);
            }
        }
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
using namespace std;

const int treeSize = 131072;
const int inf = (int) 1e9;

vector < int > change[100005], stay[100005]/*, cng[100005]*/;
int treeDown[treeSize * 2 + 5], treeUp[treeSize * 2 + 5], downCommon[treeSize * 2 + 5], upCommon[treeSize * 2 + 5];
int p[2][100005], pos[2][100005];
bool down[100005], up[100005];
int n;

void initTrees () {
	int i;

	int to = treeSize * 2 + 1;
	for (i = 1;i < to;i++) {
		treeDown[i] = inf;
		treeUp[i] = inf;
	}
}

void upTrees () {
	int i;

	for (i = treeSize - 1;i > 0;i--) {
		treeDown[i] = min(treeDown[i << 1], treeDown[(i << 1) | 1]);
		treeUp[i] = min(treeUp[i << 1], treeUp[(i << 1) | 1]);
	}
}

void add_tree_down (int v, int tl, int tr, int l, int r, int c, bool s = false) {
	if (l > r)
		return ;

	if (tl == l && tr == r) {
		if (!s) {
			downCommon[v] += c;
			treeDown[v] += c;
		}else
		{
			treeDown[v] = c;
			downCommon[v] = 0;
		}
		return ;
	}

	int vl = (v << 1), vr = ((v << 1) | 1);
	downCommon[vl] += downCommon[v]; treeDown[vl] += downCommon[v];
	downCommon[vr] += downCommon[v]; treeDown[vr] += downCommon[v];
	downCommon[v] = 0;

	int mid = (tl + tr) / 2;

	add_tree_down(vl, tl, mid, l, min(mid, r), c, s);
	add_tree_down(vr, mid + 1, tr, max(l, mid + 1), r, c, s);

	treeDown[v] = min(treeDown[vl], treeDown[vr]);
}

void add_tree_up (int v, int tl, int tr, int l, int r, int c, bool s = false) {
	if (l > r)
		return ;

	if (tl == l && tr == r) {
		if (!s) {
			upCommon[v] += c;
			treeUp[v] += c;
		}else
		{
			treeUp[v] = c;
			upCommon[v] = 0;
		}
		return ;
	}

	int vl = (v << 1), vr = ((v << 1) | 1);
	upCommon[vl] += upCommon[v]; treeUp[vl] += upCommon[v];
	upCommon[vr] += upCommon[v]; treeUp[vr] += upCommon[v];
	upCommon[v] = 0;

	int mid = (tl + tr) / 2;

	add_tree_up(vl, tl, mid, l, min(mid, r), c, s);
	add_tree_up(vr, mid + 1, tr, max(l, mid + 1), r, c, s);

	treeUp[v] = min(treeUp[vl], treeUp[vr]);
}

void addTreeDown (int l, int r, int c, bool s = false) {
	add_tree_down(1, 0, treeSize - 1, l, r, c, s);
}

void addTreeUp (int l, int r, int c, bool s = false) {
	add_tree_up(1, 0, treeSize - 1, l, r, c, s);
}

void solve (int ind) {
	int i, cur;
	int temp;

	for (i = 0;i < change[ind].size();i++) {
		cur = change[ind][i];

		if (down[cur]) {
			addTreeDown(cur, cur, inf, true);
		}else
		{
			addTreeUp(cur, cur, inf, true);
		}
	}
	for (i = 0;i < stay[ind].size();i++) {
		cur = stay[ind][i];

		if (down[cur]) {
			addTreeDown(cur, cur, inf, true);
		}else
		{
			addTreeUp(cur, cur, inf, true);
		}
	}

	addTreeDown(0, treeSize - 1, -1);
	addTreeUp(0, treeSize - 1, 1);

	for (i = 0;i < change[ind].size();i++) {
		cur = change[ind][i];

		if (down[cur]) {
			temp = 0;
			addTreeUp(cur, cur, temp, true);
		}else
		{
			temp = n - 1 - pos[0][cur];
			addTreeDown(cur, cur, temp, true);
		}

		swap(down[cur], up[cur]);
	}
	for (i = 0;i < stay[ind].size();i++) {
		cur = stay[ind][i];

		if (down[cur])
			throw 42;

		temp = n - 1 - pos[0][cur];
		addTreeUp(cur, cur, temp, true);
	}

	printf("%d", min(treeDown[1], treeUp[1]));
}

int main () {
	//freopen("input.txt", "rt", stdin);
	//freopen("output.txt", "wt", stdout);
	int k, i;

	scanf("%d", &n);
	for (k = 0;k < 2;k++) {
		for (i = 0;i < n;i++) {
			scanf("%d", &p[k][i]);
			p[k][i]--;
			pos[k][p[k][i]] = i;
		}
	}

	initTrees();

	for (i = 0;i < n;i++) {
		if (pos[1][i] > pos[0][i]) {
			treeDown[treeSize + i] = pos[1][i] - pos[0][i];
			down[i] = true;
		}else
		{
			treeUp[treeSize + i] = pos[0][i] - pos[1][i];
			up[i] = true;
		}
	}

	upTrees();

	int fir, sec;
	for (i = 0;i < n;i++) {
		fir = pos[1][i];

		if (pos[1][i] > pos[0][i])
			sec = pos[1][i] - pos[0][i] - 1;
		else
			sec = pos[1][i] + (n - 1 - pos[0][i]);

		if (fir != sec) {
			change[fir].push_back(i);
			change[sec].push_back(i);
			//cng[fir].push_back(i);
			//cng[sec].push_back(i);
		}else
		{
			stay[fir].push_back(i);
			//cng[fir].push_back(i);
		}
	}

	printf("%d", min(treeDown[1], treeUp[1]));

	for (i = 0;i < n - 1;i++) {
		printf("\n");
		solve(i);
	}

	return 0;
}


#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#define ll long long
#define PI acos(-1.0)
#define eps 1e-9
#define MOD 1000000007
#define INF 1000000000
using namespace std;
template<class T> T f_min(T x,T y){return x<y?x:y;}
template<class T> T f_max(T x,T y){return x>y?x:y;}
template<class T> T f_abs(T x){return x<0?-x:x;}
int N,A[100005],B[100005],loc[100005];
int up[100005],un;
int dw[100005],dn,vmin[100005];
void get_data(){
    scanf("%d",&N);
    int i;
    for(i=0;i<N;i++){
        scanf("%d",&A[i]);
        loc[A[i]]=i;
    }
    un=dn=0;
    int a,b;
    for(i=0;i<N;i++){
        scanf("%d",&B[i]);
        up[un++]=(loc[B[i]]-i+N)%N;
        dw[dn++]=(i-loc[B[i]]+N)%N;
    }
    sort(up,up+un);
    un=unique(up,up+un)-up;

    sort(dw,dw+dn);
    dn=unique(dw,dw+dn)-dw;

    memset(vmin,-1,sizeof vmin);
}

void update(int &x,int v){
    if(x==-1||x>v)x=v;
}
int cal_up(int x,int t){
    x=(x+t)%N;
    return f_min(x,N-x);
}
int cal_dw(int x,int t){
    x=((x-t)%N+N)%N;
    return f_min(x,N-x);
}
void run(){
    int i,now=0;

    while(un&&up[un-1]>N/2)un--;
    while(dn&&dw[dn-1]>N/2)dn--;
    while(!un&&!dn);
    if(un){
    for(i=0;i<N;i++){
        while(cal_up(up[now],i)>cal_up(up[(now-1+un)%un],i)){
            now=(now-1+un)%un;
        }
        update(vmin[i],cal_up(up[now],i));
    }
    }
    if(dn){
    now=0;
    for(i=0;i<N;i++){
        while(cal_dw(dw[now],i)>cal_dw(dw[(now+1)%dn],i)){
            now=(now+1)%dn;
        }
        update(vmin[i],cal_dw(dw[now],i));
    }
    }
    for(i=0;i<N;i++){
        while(vmin[i]==-1);
        printf("%d\n",vmin[i]);
    }

}
int main(){
    get_data();
    run();
    return 0;
}


#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#define ll long long
#define PI acos(-1.0)
#define eps 1e-9
#define MOD 1000000007
#define INF 1000000000
using namespace std;
template<class T> T f_min(T x,T y){return x<y?x:y;}
template<class T> T f_max(T x,T y){return x>y?x:y;}
template<class T> T f_abs(T x){return x<0?-x:x;}
int N,A[100005],B[100005],loc[100005];
int up[100005],un;
int dw[100005],dn,vmin[1000005];
void get_data(){
    scanf("%d",&N);
    int i;
    for(i=0;i<N;i++){
        scanf("%d",&A[i]);
        loc[A[i]]=i;
    }
    dn=dn=0;
    int a,b;
    for(i=0;i<N;i++){
        scanf("%d",&B[i]);
        up[un++]=(loc[B[i]]-i+N)%N;
        dw[dn++]=(i-loc[B[i]]+N)%N;
    }
    sort(up,up+un);
    un=unique(up,up+un)-up;

    sort(dw,dw+dn);
    dn=unique(dw,dw+dn)-dw;

    memset(vmin,0x3f,sizeof vmin);
}

void update(int &x,int v){
    x=x<v?x:v;
}
int cal_up(int x,int t){
    x=(x+t)%N;
    return f_min(x,N-x);
}
int cal_dw(int x,int t){
    x=(x-t+N)%N;
    return f_min(x,N-x);
}
void run(){
    int i,now=0;

    while(up[un-1]>N/2)un--;
    while(dw[dn-1]>N/2)dn--;
    for(i=0;i<N;i++){
        if(cal_up(up[now],i)>cal_up(up[(now-1+N)%N],i)){
            now=(now-1+N)%N;
        }
        update(vmin[i],cal_up(up[now],i));
    }

    now=0;
    for(i=0;i<N;i++){
        if(cal_dw(dw[now],i)>cal_dw(dw[(now+1)%N],i)){
            now=(now+1)%N;
        }
        update(vmin[i],cal_dw(dw[now],i));
    }

    for(i=0;i<N;i++)printf("%d\n",vmin[i]);

}
int main(){
    get_data();
    run();
    return 0;
}


#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#define ll long long
#define PI acos(-1.0)
#define eps 1e-9
#define MOD 1000000007
#define INF 1000000000
using namespace std;
template<class T> T f_min(T x,T y){return x<y?x:y;}
template<class T> T f_max(T x,T y){return x>y?x:y;}
template<class T> T f_abs(T x){return x<0?-x:x;}
int N,A[100005],B[100005],loc[100005];
int up[100005],un;
int dw[100005],dn,vmin[1000005];
void get_data(){
    scanf("%d",&N);
    int i;
    for(i=0;i<N;i++){
        scanf("%d",&A[i]);
        loc[A[i]]=i;
    }
    dn=dn=0;
    int a,b;
    for(i=0;i<N;i++){
        scanf("%d",&B[i]);
        up[un++]=(loc[B[i]]-i+N)%N;
        dw[dn++]=(i-loc[B[i]]+N)%N;
    }
    sort(up,up+un);
    un=unique(up,up+un)-up;

    sort(dw,dw+dn);
    dn=unique(dw,dw+dn)-dw;

    memset(vmin,0x3f,sizeof vmin);
}

void update(int &x,int v){
    x=x<v?x:v;
}
int cal_up(int x,int t){
    x=(x+t)%N;
    return f_min(x,N-x);
}
int cal_dw(int x,int t){
    x=(x-t+N)%N;
    return f_min(x,N-x);
}
void run(){
    int i,now=0;

    while(up[un-1]>N/2)un--;
    while(dw[dn-1]>N/2)dn--;
    if(un){
    for(i=0;i<N;i++){
        while(cal_up(up[now],i)>cal_up(up[(now-1+un)%un],i)){
            now=(now-1+un)%un;
        }
        update(vmin[i],cal_up(up[now],i));
    }
    }
    if(dn){
    now=0;
    for(i=0;i<N;i++){
        while(cal_dw(dw[now],i)>cal_dw(dw[(now+1)%dn],i)){
            now=(now+1)%dn;
        }
        update(vmin[i],cal_dw(dw[now],i));
    }
    }
    for(i=0;i<N;i++)printf("%d\n",vmin[i]);

}
int main(){
    get_data();
    run();
    return 0;
}


#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#define ll long long
#define PI acos(-1.0)
#define eps 1e-9
#define MOD 1000000007
#define INF 1000000000

#define MP make_pair
#define PB push_back
using namespace std;
template<class T> T f_min(T x,T y){return x<y?x:y;}
template<class T> T f_max(T x,T y){return x>y?x:y;}
template<class T> T f_abs(T x){return x<0?-x:x;}
int N,A[100005],B[100005],la[100005],lb[100005];
vector<pair<int,int> >pr1[100005],pr2[100005];
void f_add1(int S,int E,int sv){
    pr1[S].PB(MP(sv+S,1));
    pr1[E+1].PB(MP(sv+S,-1));
}
void f_add2(int S,int E,int sv){
    pr2[S].PB(MP(sv-S,1));
    pr2[E+1].PB(MP(sv-S,-1));
}
void get_data(){
    int i;
    scanf("%d",&N);
    for(i=0;i<N;i++)pr1[i].clear(),pr2[i].clear();
    for(i=0;i<N;i++){
        scanf("%d",&A[i]);A[i]--;
        la[A[i]]=i;
    }
    for(i=0;i<N;i++){
        scanf("%d",&B[i]);B[i]--;
        lb[B[i]]=i;
    }
    for(i=0;i<N;i++){
        if(la[i]<lb[i]){
            f_add1(0,lb[i]-la[i],lb[i]-la[i]);//S E sv
            f_add1(lb[i]+1,N-1,N-1-la[i]);

            f_add2(lb[i]-la[i],lb[i],0);

        }else{
            f_add2(0,lb[i],la[i]-lb[i]);
            f_add2(lb[i]+1+N-la[i]-1,N-1,0);

            f_add1(lb[i]+1,lb[i]+1+N-la[i]-1,N-1-la[i]);
        }
    }
}
multiset<int>mst;
int ans[100005];
void run(){//printf("*");
    int i,j;
    mst.clear();
    memset(ans,0x3f,sizeof ans);
    multiset<int>::iterator it;
    for(i=0;i<N;i++){//printf("<%d>\n",i);
        for(j=0;j<pr1[i].size();j++){
            if(pr1[i][j].second==1)mst.insert(pr1[i][j].first);
            else mst.erase(mst.find(pr1[i][j].first));
        }
        if(mst.size()){
            it=mst.begin();
            ans[i]=f_min(ans[i],(*it)-i);
        }
    }

    mst.clear();
    for(i=0;i<N;i++){
        for(j=0;j<pr2[i].size();j++){
            if(pr2[i][j].second==1)mst.insert(pr2[i][j].first);
            else mst.erase(mst.find(pr2[i][j].first));
        }
        if(mst.size()){
            it=mst.begin();
            ans[i]=f_min(ans[i],(*it)+i);
        }
    }

    for(i=0;i<N;i++)printf("%d\n",ans[i]);
}
int main(){
    get_data();
    run();
    return 0;
}


#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#define ll long long
#define PI acos(-1.0)
#define eps 1e-9
#define MOD 1000000007
#define INF 1000000000
using namespace std;
template<class T> T f_min(T x,T y){return x<y?x:y;}
template<class T> T f_max(T x,T y){return x>y?x:y;}
template<class T> T f_abs(T x){return x<0?-x:x;}
int N,A[100005],B[100005],loc[100005];
int up[100005],un;
int dw[100005],dn,vmin[1000005];
void get_data(){
    scanf("%d",&N);
    int i;
    for(i=0;i<N;i++){
        scanf("%d",&A[i]);
        loc[A[i]]=i;
    }
    dn=dn=0;
    int a,b;
    for(i=0;i<N;i++){
        scanf("%d",&B[i]);
        up[un++]=(loc[B[i]]-i+N)%N;
        dw[dn++]=(i-loc[B[i]]+N)%N;
    }
    sort(up,up+un);
    un=unique(up,up+un)-up;

    sort(dw,dw+dn);
    dn=unique(dw,dw+dn)-dw;

    memset(vmin,0x3f,sizeof vmin);
}

void update(int &x,int v){
    x=x<v?x:v;
}
int cal_up(int x,int t){
    x=(x+t)%N;
    return f_min(x,N-x);
}
int cal_dw(int x,int t){
    x=(x-t+N)%N;
    return f_min(x,N-x);
}
void run(){
    int i,now=0;

    while(up[un-1]>N/2)un--;
    while(dw[dn-1]>N/2)dn--;
    if(un){
    for(i=0;i<N;i++){
        if(cal_up(up[now],i)>cal_up(up[(now-1+un)%un],i)){
            now=(now-1+un)%un;
        }
        update(vmin[i],cal_up(up[now],i));
    }
    }
    if(dn){
    now=0;
    for(i=0;i<N;i++){
        if(cal_dw(dw[now],i)>cal_dw(dw[(now+1)%dn],i)){
            now=(now+1)%dn;
        }
        update(vmin[i],cal_dw(dw[now],i));
    }
    }
    for(i=0;i<N;i++)printf("%d\n",vmin[i]);

}
int main(){
    get_data();
    run();
    return 0;
}


#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#define ll long long
#define PI acos(-1.0)
#define eps 1e-9
#define MOD 1000000007
#define INF 1000000000
using namespace std;
template<class T> T f_min(T x,T y){return x<y?x:y;}
template<class T> T f_max(T x,T y){return x>y?x:y;}
template<class T> T f_abs(T x){return x<0?-x:x;}
int N,A[100005],B[100005],loc[100005];
int up[100005],un;
int dw[100005],dn,vmin[100005];
void get_data(){
    scanf("%d",&N);
    int i;
    for(i=0;i<N;i++){
        scanf("%d",&A[i]);
        loc[A[i]]=i;
    }
    un=dn=0;
    int a,b;
    for(i=0;i<N;i++){
        scanf("%d",&B[i]);
        up[un++]=(loc[B[i]]-i+N)%N;
        dw[dn++]=(i-loc[B[i]]+N)%N;
    }
    sort(up,up+un);
    un=unique(up,up+un)-up;

    sort(dw,dw+dn);
    dn=unique(dw,dw+dn)-dw;

    memset(vmin,-1,sizeof vmin);
}

void update(int &x,int v){
    if(x==-1||x>v)x=v;
}
int cal_up(int x,int t){
    x=(x+t)%N;
    return f_min(x,N-x);
}
int cal_dw(int x,int t){
    x=((x-t)%N+N)%N;
    return f_min(x,N-x);
}
void run(){
    int i,now=0;

    while(un&&up[un-1]>N/2)un--;
    while(dn&&dw[dn-1]>N/2)dn--;
    while(!un&&!dn);
    if(un){
        for(i=0;i<N;i++){
            while(cal_up(up[now],i)>=cal_up(up[(now-1+un)%un],i)){
                now=(now-1+un)%un;
            }
            update(vmin[i],cal_up(up[now],i));
        }
    }
    if(dn){
        now=0;
        for(i=0;i<N;i++){
            while(cal_dw(dw[now],i)>=cal_dw(dw[(now+1)%dn],i)){
                now=(now+1)%dn;
            }
            update(vmin[i],cal_dw(dw[now],i));
        }
    }
    for(i=0;i<N;i++){
        while(vmin[i]==-1);
        printf("%d\n",vmin[i]);
    }

}
int main(){
//  freopen("1.txt","r",stdin);
//  freopen("2.txt","w",stdout);
    get_data();
    run();
    return 0;
}


#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#define ll long long
#define PI acos(-1.0)
#define eps 1e-9
#define MOD 1000000007
#define INF 1000000000
using namespace std;
template<class T> T f_min(T x,T y){return x<y?x:y;}
template<class T> T f_max(T x,T y){return x>y?x:y;}
template<class T> T f_abs(T x){return x<0?-x:x;}
int N,A[100005],B[100005],loc[100005];
int up[100005],un;
int dw[100005],dn,vmin[1000005];
void get_data(){
    scanf("%d",&N);
    int i;
    for(i=0;i<N;i++){
        scanf("%d",&A[i]);
        loc[A[i]]=i;
    }
    un=dn=0;
    int a,b;
    for(i=0;i<N;i++){
        scanf("%d",&B[i]);
        up[un++]=(loc[B[i]]-i+N)%N;
        dw[dn++]=(i-loc[B[i]]+N)%N;
    }
    sort(up,up+un);
    un=unique(up,up+un)-up;

    sort(dw,dw+dn);
    dn=unique(dw,dw+dn)-dw;

    memset(vmin,0x3f,sizeof vmin);
}

void update(int &x,int v){
    x=x<v?x:v;
}
int cal_up(int x,int t){
    x=(x+t)%N;
    return f_min(x,N-x);
}
int cal_dw(int x,int t){
    x=(x-t+N)%N;
    return f_min(x,N-x);
}
void run(){
    int i,now=0;

    while(up[un-1]>N/2)un--;
    while(dw[dn-1]>N/2)dn--;
    if(un){
    for(i=0;i<N;i++){
        while(cal_up(up[now],i)>cal_up(up[(now-1+un)%un],i)){
            now=(now-1+un)%un;
        }
        update(vmin[i],cal_up(up[now],i));
    }
    }
    if(dn){
    now=0;
    for(i=0;i<N;i++){
        while(cal_dw(dw[now],i)>cal_dw(dw[(now+1)%dn],i)){
            now=(now+1)%dn;
        }
        update(vmin[i],cal_dw(dw[now],i));
    }
    }
    for(i=0;i<N;i++)printf("%d\n",vmin[i]);

}
int main(){
    get_data();
    run();
    return 0;
}


#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#define ll long long
#define PI acos(-1.0)
#define eps 1e-9
#define MOD 1000000007
#define INF 1000000000
using namespace std;
template<class T> T f_min(T x,T y){return x<y?x:y;}
template<class T> T f_max(T x,T y){return x>y?x:y;}
template<class T> T f_abs(T x){return x<0?-x:x;}
int N,A[100005],B[100005],loc[100005];
int up[100005],un;
int dw[100005],dn,vmin[1000005];
void get_data(){
    scanf("%d",&N);
    int i;
    for(i=0;i<N;i++){
        scanf("%d",&A[i]);
        loc[A[i]]=i;
    }
    un=dn=0;
    int a,b;
    for(i=0;i<N;i++){
        scanf("%d",&B[i]);
        up[un++]=(loc[B[i]]-i+N)%N;
        dw[dn++]=(i-loc[B[i]]+N)%N;
    }
    sort(up,up+un);
    un=unique(up,up+un)-up;

    sort(dw,dw+dn);
    dn=unique(dw,dw+dn)-dw;

    memset(vmin,-1,sizeof vmin);
}

void update(int &x,int v){
    if(x==-1||x>v)x=v;
}
int cal_up(int x,int t){
    x=(x+t)%N;
    return f_min(x,N-x);
}
int cal_dw(int x,int t){
    x=((x-t)%N+N)%N;
    return f_min(x,N-x);
}
void run(){
    int i,now=0;

    while(up[un-1]>N/2)un--;
    while(dw[dn-1]>N/2)dn--;
    if(un){
    for(i=0;i<N;i++){
        while(cal_up(up[now],i)>cal_up(up[(now-1+un)%un],i)){
            now=(now-1+un)%un;
        }
        update(vmin[i],cal_up(up[now],i));
    }
    }
    if(dn){
    now=0;
    for(i=0;i<N;i++){
        while(cal_dw(dw[now],i)>cal_dw(dw[(now+1)%dn],i)){
            now=(now+1)%dn;
        }
        update(vmin[i],cal_dw(dw[now],i));
    }
    }
    for(i=0;i<N;i++){
        while(vmin[i]==-1);
        printf("%d\n",vmin[i]);
    }

}
int main(){
    get_data();
    run();
    return 0;
}