import java.io.*;
import java.util.*;
import java.math.*;

public class C {
	static BufferedReader in;
	static PrintWriter out;
	static StringTokenizer st;
	static Random rnd;

	class Pair {
		int num, distance;

		public Pair(int num, int distance) {
			this.num = num;
			this.distance = distance;
		}
	}

	class SmartSet {
		TreeSet<Pair> set;
		Pair[] pairs;
		int diff;

		public SmartSet(int n) {
			set = new TreeSet<Pair>(new Comparator<Pair>() {
				public int compare(Pair a, Pair b) {
					if (a.distance != b.distance)
						return Integer.signum(a.distance - b.distance);

					return Integer.signum(a.num - b.num);
				}
			});
			pairs = new Pair[n];
			diff = 0;
		}

		public void remove(int num) {
			if (pairs[num] != null)
				set.remove(pairs[num]);
			pairs[num] = null;
		}

		public void add(int num, int distance) {
			int fixedDistance = distance - diff;
			pairs[num] = new Pair(num, fixedDistance);
			set.add(pairs[num]);
		}

		public void increaseAll() {
			++diff;
		}

		public void decreaseAll() {
			--diff;
		}

		public int getMin() {
			if (set.size() == 0)
				return Integer.MAX_VALUE;

			return set.first().distance + diff;
		}

		public int poll() {
			if (set.size() > 0 && set.first().distance + diff < 0) {
				int num = set.pollFirst().num;
				pairs[num] = null;
				return num;
			}

			return -1;
		}
	}

	void solve() throws IOException {
		int n = nextInt();

		int[] perm = new int[n], poses = new int[n];

		for (int i = 0; i < n; i++) {
			perm[i] = nextInt() - 1;
			poses[perm[i]] = i;
		}

		SmartSet leftSet = new SmartSet(n), rightSet = new SmartSet(n);

		int[] secondPerm = new int[n], secondPoses = new int[n];

		for (int i = 0; i < n; i++) {
			secondPerm[i] = nextInt() - 1;
			secondPoses[secondPerm[i]] = i;

			int posInFirstPerm = poses[secondPerm[i]];

			if (posInFirstPerm > i) {
				leftSet.add(secondPerm[i], Math.abs(posInFirstPerm - i));
			} else {
				rightSet.add(secondPerm[i], Math.abs(posInFirstPerm - i));
			}
		}

		for (int i = 0; i < n; i++) {
			out.println(Math.min(leftSet.getMin(), rightSet.getMin()));

			leftSet.increaseAll();
			rightSet.decreaseAll();

			while (true) {
				int polledNum = rightSet.poll();

				if (polledNum == -1)
					break;

				leftSet.add(polledNum, 1);
			}

			int curNumToShift = secondPerm[i];
			leftSet.remove(curNumToShift);
			rightSet.remove(curNumToShift);

			rightSet.add(curNumToShift, Math.abs(n - 1 - poses[curNumToShift]));

		}

	}

	public static void main(String[] args) {
		new C().run();
	}

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);

			rnd = new Random();

			solve();

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(42);
		}
	}

	String nextToken() throws IOException {
		while (st == null || !st.hasMoreTokens()) {
			String line = in.readLine();

			if (line == null)
				return null;

			st = new StringTokenizer(line);
		}

		return st.nextToken();
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}


import java.util.Scanner;
import java.util.TreeMap;
public class Shift220C {
	class MAP {
		TreeMap<Integer, Integer> mp = new TreeMap<Integer, Integer>();
		void add(int k, int t) {
			if (t == 0)
				return;
			if (mp.containsKey(k))
				mp.put(k, t + mp.get(k));
			else
				mp.put(k, t);
		}
		void del(int k, int t) {
			if (t == 0)
				return;
			if (!mp.containsKey(k))
				return;
			if (mp.get(k) == t)
				mp.remove(k);
			else
				mp.put(k, mp.get(k) - t);
		}
		int getmin() {
			if (mp.isEmpty())
				return inf;
			return mp.firstKey();
		}
		int getmax() {
			if (mp.isEmpty())
				return -inf;
			return mp.lastKey();
		}
	}
	int inf = 1 << 28, maxn = 100010;
	MAP neg = new MAP(), pos = new MAP();
	int pa[] = new int[maxn], b[] = new int[maxn];
	int num[] = new int[maxn * 2], x[] = new int[maxn];
	Scanner scan = new Scanner(System.in);
	void run() {
		int n = scan.nextInt();
		for (int i = 1; i <= n; i++)
			pa[scan.nextInt()] = i;
		for (int i = 1; i <= n; i++) {
			b[i] = scan.nextInt();
			x[i] = i - pa[b[i]];
			if (x[i] < 0)
				neg.add(x[i], 1);
			else {
				num[x[i]]++;
				pos.add(x[i], 1);
			}
		}
		System.out.println(Math.min(-neg.getmax(), pos.getmin()));
		for (int i = 1; i < n; i++) {
			if (x[i] + 1 < i)
				neg.del(x[i], 1);
			else {
				pos.del(x[i], 1);
				num[x[i]]--;
			}
			int temp = n - pa[b[i]] + i;
			pos.add(temp, 1);
			num[temp]++;
			pos.del(i - 1, num[i - 1]);
			neg.add(i - 1, num[i - 1]);
			System.out.println(Math.min(-neg.getmax() + i, pos.getmin() - i));
		}
	}

	public static void main(String[] args) {
		new Shift220C().run();
	}
}


import java.io.*;
import java.util.*;

public class C {

	static final int INF = Integer.MAX_VALUE / 2;
	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;
	boolean eof;

	int f(int x) {
		while (x < 0)
			x += n;
		return x;
	}

	int n;

	void solve() throws IOException {
		n = nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[nextInt() - 1] = i;
		int[] b = new int[n];
		for (int i = 0; i < n; i++)
			b[nextInt() - 1] = i;

		if (n == 1) {
			out.println(0);
			return;
		}

		int[] ans = new int[n];
		Arrays.fill(ans, INF);
		for (int i = 0; i < n; i++)
			ans[f(b[i] - a[i])] = 0;

		int zero = -1;
		for (int i = 0; i < n; i++)
			if (ans[i] == 0) {
				zero = i;
				break;
			}

		for (int i = zero + 1, j = 1; i != zero; i++, j++) {
			if (i == n)
				i = 0;
			if (ans[i] == 0)
				j = 0;
			ans[i] = Math.min(ans[i], j);
		}

		for (int i = zero - 1, j = 1; true; i--, j++) {
			if (i == -1)
				i = n - 1;
			if (i == zero)
				break;
			if (ans[i] == 0)
				j = 0;
			ans[i] = Math.min(ans[i], j);
		}

		for (int i = 0; i < n; i++)
			out.println(ans[i]);
	}

	void inp() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new C().inp();
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return null;
			}
		}
		return st.nextToken();
	}

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			eof = true;
			return null;
		}
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}


import java.io.*;
import java.util.*;

public class C {

	static final int INF = Integer.MAX_VALUE / 2;
	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;
	boolean eof;

	int n;

	int f(int x) {
		return x < 0 ? x + n : x;
	}

	void solve() throws IOException {
		n = nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[nextInt() - 1] = i;
		int[] b = new int[n];
		for (int i = 0; i < n; i++)
			b[nextInt() - 1] = i;

		for (int i = 0; i < n; i++) {

			int ans = INF;
			for (int j = 0; j < n; j++)
				ans = Math.min(ans, Math.abs(a[j] - f(b[j] - i)));
			out.println(ans);
		}
	}

	void inp() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new C().inp();
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return null;
			}
		}
		return st.nextToken();
	}

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			eof = true;
			return null;
		}
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}


import java.io.*;
import java.util.*;

public class C {

	static final int INF = Integer.MAX_VALUE / 2;
	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;
	boolean eof;

	void solve() throws IOException {
		int n = nextInt();
		final int[] a = new int[n + 1];
		for (int i = 0; i < n; i++)
			a[nextInt() - 1] = i;
		final int[] b = new int[n + 1];
		for (int i = 0; i < n; i++)
			b[nextInt() - 1] = i;

		int[] nowChange = new int[n];
		for (int i = 0; i < n; i++)
			nowChange[b[i]] = i;

		TreeSet<Integer> set = new TreeSet<Integer>(new Comparator<Integer>() {

			@Override
			public int compare(Integer o1, Integer o2) {
				if (a[o1] - b[o1] != a[o2] - b[o2])
					return Integer.compare(a[o1] - b[o1], a[o2] - b[o2]);
				return Integer.compare(o1, o2);
			}
		});

		for (int i = 0; i < n; i++)
			set.add(i);

		for (int i = 0; i < n; i++) {
			int ans = INF;
			Integer low = set.lower(n);
			if (low != null)
				ans = Math.min(ans, -(a[low] - b[low] + i));

			Integer high = set.higher(n);
			if (high != null)
				ans = Math.min(ans, a[high] - b[high] + i);

			//System.err.println(low + " " + high);

			out.println(ans);

			int tmp = nowChange[i];
			set.remove(tmp);
			b[tmp] += n;
			set.add(tmp);

			b[n]++;
		}
	}

	void inp() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new C().inp();
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return null;
			}
		}
		return st.nextToken();
	}

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			eof = true;
			return null;
		}
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}


import java.io.*;
import java.util.*;

public class C {

	static final int INF = Integer.MAX_VALUE / 2;
	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;
	boolean eof;

	int f(int x) {
		while (x < 0)
			x += n;
		return x;
	}

	int n;

	void solve() throws IOException {
		n = nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[nextInt() - 1] = i;
		int[] b = new int[n];
		for (int i = 0; i < n; i++)
			b[nextInt() - 1] = i;

		if (n == 1) {
			out.println(0);
			return;
		}

		int[] ans = new int[n];
		Arrays.fill(ans, INF);
		for (int i = 0; i < n; i++)
			ans[f(b[i] - a[i])] = 0;

		int zero = -1;
		for (int i = 0; i < n; i++)
			if (ans[i] == 0) {
				zero = i;
				break;
			}

		for (int i = zero + 1, j = 1; true; i++, j++) {
			if (i == n)
				i = 0;
			if (i == zero)
				break;
			if (ans[i] == 0)
				j = 0;
			ans[i] = Math.min(ans[i], j);
		}

		for (int i = zero - 1, j = 1; true; i--, j++) {
			if (i == -1)
				i = n - 1;
			if (i == zero)
				break;
			if (ans[i] == 0)
				j = 0;
			ans[i] = Math.min(ans[i], j);
		}

		for (int i = 0; i < n; i++)
			out.println(ans[i]);
	}

	void inp() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new C().inp();
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return null;
			}
		}
		return st.nextToken();
	}

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			eof = true;
			return null;
		}
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}


#include <bits/stdc++.h>
using namespace std;
#define pii             pair<int , int >
#define inf             1111111111
#define in(a)           scanf("%d", &a)
#define ins(a)          scanf("%s", a)
#define in2(a, b)       scanf("%d%d", &a, &b)
#define in3(a, b, c)    scanf("%d%d%d", &a, &b, &c)
#define mp              make_pair
#define vi              vector<int >
#define _ceil(n, a)     ((n)%(a)==0?((n)/(a)):((n)/(a)+1))
#define cl              clear()
#define sz              size()
#define pn              printf("\n")
#define pr(a)           printf("%d\n", a)
#define prs(a)          printf("%d ", a)
#define pr2(a, b)       printf("%d %d\n", a, b)
#define pr3(a, b, c)    printf("%d %d %d\n", a, b, c)
#define pb              push_back
#define mem(a, b)       memset((a), (b), sizeof(a))
#define all(X)          (X).begin(), (X).end ()
#define iter(it, X)     for (__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++)
#define ext(a)          {printf("%s\n", a); return 0;}
#define oka(x, y)       ((x)>=0&&(x)<row&&(y)>=0&&(y)<col)
#define x               first
#define y               second
#define lc              (2*i)
#define rc              (2*i+1)
#define sst             clock_t t = clock()
#define eed             printf ("It took me %d clicks.\n", (int)(clock() - t))

//#define prr(args...) { vector<string> _v = split(#args, ','); err(_v.begin(), args); pn;}
//vector<string> split(const string& s, char c) {
//	vector<string> v;
//	stringstream ss(s);
//	string x;
//	while (getline(ss, x, c))
//		v.emplace_back(x);
//	return move(v);
//}
//void err(vector<string>::iterator it) {}
//template<typename T, typename... Args>
//void err(vector<string>::iterator it, T a, Args... args) {
//	cerr <<a<<" ";
//	err(++it, args...);
//}

typedef long long LL;
//int  dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int  dx[]={1,1,0,-1,-1,-1,0,1,0};int dy[]={0,1,1,1,0,-1,-1,-1,0};//8 direction
//int  dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
//bool check(int n, int pos) {return (n & (1<<pos))>>pos;} //typecast 1 in case of int
//int  on(int n, int pos) {return n | (1<<pos);} //typecast 1 in case of int
//int  off(int n, int pos) {return n & ~(1<<pos);} //typecast 1 in case of int
//bool operator < (const data &d) const{return cost<d.cost;} //reverse in priority queue

const int M = 300010, mod = 1000000007;

int actual[M], A[M];
multiset<int>S;

int minimum(int moves)
{
    multiset<int>::iterator it;
    it = S.lower_bound(-moves);
    if (it == S.begin()) return abs(*it + moves);

    int temp = *it;
    it--;
    return min(abs(temp + moves), abs(*it + moves));
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:\\Users\\Dell\\Desktop\\in.txt", "r", stdin);
//    freopen("C:\\Users\\Dell\\Desktop\\in.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);

    int i, j, k, n;

    in(n);
    for (i=1; i<=n; i++) in(k), actual[k] = i;
    for (i=1; i<=n; i++) in(A[i]), A[i] = actual[A[i]], S.insert(A[i] - i);

    pr(minimum(0));
    for (i=1; i<n; i++)
    {
        multiset<int>::iterator it = S.lower_bound(A[i] - i);
        S.erase(it);
        S.insert(A[i] - n - i);

        pr(minimum(i));
    }

return 0;
}


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

#define scl(x) scanf("%lld",&x)
#define sc(x)  scanf("%d",&x)
#define ll long long
#define lop(i,n) for(int i=0;i<n;++i)
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;

int n,a[100100],b[100100],idx[100100],c1,c2;
priority_queue<ii,vector<ii> ,greater<ii> > ql,qr;
int main(){
#ifndef ONLINE_JUDGE
	freopen("i.txt","r",stdin);
#endif
	sc(n);
	lop(i,n){
		sc(a[i]);--a[i];
		idx[a[i]]=i;
	}
	int out=n-1;
	lop(i,n){
		sc(b[i]),--b[i],out=min(out,abs(i-idx[b[i]]));
		if(i>idx[b[i]])qr.push(ii(i-idx[b[i]],b[i]));
		else ql.push(ii(idx[b[i]]-i,b[i]));
	}
	cout<<out<<endl;
	lop(i,n-1){
		++c1,--c2;
		if(a[n-1]==b[i])ql.push(ii(-c1,b[i]));
		else qr.push(ii(n-1-idx[b[i]]-c2,b[i]));

		while(qr.size()&&!(qr.top().first+c2)){
			ql.push(ii(-c1,qr.top().second));
			qr.pop();
		}

		int ot=n-1;
		if(ql.size())ot=c1+ql.top().first;
		if(qr.size())ot=min(ot,c2+qr.top().first);
		printf("%d\n",ot);
	}

}


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

#define scl(x) scanf("%lld",&x)
#define sc(x)  scanf("%d",&x)
#define ll long long
#define lop(i,n) for(int i=0;i<n;++i)
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;

int n,a[100100],b[100100],idx[100100],isShifted[100100],c1,c2;
priority_queue<ii,vector<ii> ,greater<ii> > ql,qr;
int main(){
#ifndef ONLINE_JUDGE
	freopen("i.txt","r",stdin);
#endif
	sc(n);
	lop(i,n){
		sc(a[i]);--a[i];
		idx[a[i]]=i;
	}
	int out=n-1;
	lop(i,n){
		sc(b[i]),--b[i],out=min(out,abs(i-idx[b[i]]));
		if(i>idx[b[i]])qr.push(ii(i-idx[b[i]],b[i]));
		else ql.push(ii(idx[b[i]]-i,b[i]));
	}
	cout<<out<<endl;
	lop(i,n-1){
		++c1,--c2;

		isShifted[b[i]]=1;
		if(a[n-1]==b[i])ql.push(ii(-c1,b[i]));
		else qr.push(ii(n-1-idx[b[i]]-c2,b[i]));

		while(qr.size()&&!(qr.top().first+c2)){
			ql.push(ii(-c1,qr.top().second));
			qr.pop();
		}

		while(ql.size()&&isShifted[ql.top().second]){
			isShifted[ql.top().second]=0;
			ql.pop();
		}
		int out=n-1;
		if(ql.size())out=c1+ql.top().first;
		if(qr.size())out=min(out,c2+qr.top().first);
		printf("%d\n",out);
	}

}


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

#define scl(x) scanf("%lld",&x)
#define sc(x)  scanf("%d",&x)
#define ll long long
#define lop(i,n) for(int i=0;i<n;++i)
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;

int n,a[100100],b[100100],idx[100100],c1,c2,isInR[100100];
priority_queue<ii,vector<ii> ,greater<ii> > ql,qr;
int main(){
#ifndef ONLINE_JUDGE
	freopen("i.txt","r",stdin);
#endif
	sc(n);
	lop(i,n){
		sc(a[i]);--a[i];
		idx[a[i]]=i;
	}
	int out=n-1;
	lop(i,n){
		sc(b[i]),--b[i],out=min(out,abs(i-idx[b[i]]));
		if(i>idx[b[i]])qr.push(ii(i-idx[b[i]],b[i])),isInR[b[i]]=1;
		else ql.push(ii(idx[b[i]]-i,b[i]));
	}
	cout<<out<<endl;
	lop(i,n-1){
		++c1,--c2;
		if(a[n-1]==b[i])ql.push(ii(-c1,b[i]));
		else qr.push(ii(n-1-idx[b[i]]-c2,b[i])),isInR[b[i]]=1;

		while(qr.size()&&!(qr.top().first+c2)){
			ql.push(ii(-c1,qr.top().second));
			isInR[qr.top().second]=0;
			qr.pop();
		}
		while(ql.size()&&isInR[ql.top().second])ql.pop();
		int ot=c1+ql.top().first;
		if(qr.size())ot=min(ot,c2+qr.top().first);
		printf("%d\n",ot);
	}

}


#include <cstdio>
#include <queue>
#include <vector>

using namespace std;

const int N = 1000010;
int n, d, a[N], b[N], p[N], s[N];
bool f[N];
vector<pair<int, int>> v[2][N];
vector<int> u[2][N];
priority_queue<pair<int, int>> q;

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
    for (int i = 1; i <= n; i++) p[a[i]] = i;
    for (int i = 1; i <= n; i++)
    {
        int k = b[i];
        if (i == p[k])
        {
            v[0][1].push_back({++d, 0});
            u[0][i].push_back(d);
            if (i < n) v[1][i + 1].push_back({++d, n - i});
        } else
        if (i < p[k])
        {
            v[0][1].push_back({++d, p[k] - i});
            u[0][i].push_back(d);
            if (p[k] < n)
            {
                v[1][i + 1].push_back({++d, n - p[k]});
                u[1][n + i - p[k]].push_back(d);
            }
            v[0][n + i - p[k] + 1].push_back({++d, 0});
        } else
        {
            v[1][1].push_back({++d, i - p[k]});
            u[1][i - p[k]].push_back(d);
            v[0][i - p[k] + 1].push_back({++d, 0});
            u[0][i].push_back(d);
            if (i < n) v[1][i + 1].push_back({++d, n - p[k]});
        }
    }
    for (int i = 1; i <= n; i++) s[i] = n;
    for (int i = 1; i <= n; i++)
    {
        for (auto j : v[0][i]) q.push({i - j.second, j.first});
        if (!q.empty()) s[i] = min(s[i], i - q.top().first);
        for (auto j : u[0][i]) f[j] = true;
        while (!q.empty() && f[q.top().second]) q.pop();
    }
    while (!q.empty()) q.pop();
    for (int i = 1; i <= d; i++) f[i] = false;
    for (int i = 1; i <= n; i++)
    {
        for (auto j : v[1][i]) q.push({n + 1 - i - j.second, j.first});
        if (!q.empty()) s[i] = min(s[i], n + 1 - i - q.top().first);
        for (auto j : u[1][i]) f[j] = true;
        while (!q.empty() && f[q.top().second]) q.pop();
    }
    for (int i = 1; i <= n; i++) printf("%d\n", s[i]);
    return 0;
}


#include <cstdio>
#include <queue>
#include <vector>

using namespace std;

const int N = 100010;
int n, a[N], b[N], p[N], s[N];
bool f[N];
vector<pair<int, int>> v[2][N];
vector<int> u[2][N];
priority_queue<pair<int, int>> q;

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
    for (int i = 1; i <= n; i++) p[a[i]] = i;
    for (int i = 1; i <= n; i++)
    {
        int k = b[i];
        if (i == p[k])
        {
            v[0][1].push_back({k, 0});
            u[0][i].push_back(k);
            if (i < n) v[1][i + 1].push_back({k, n - i});
        } else
        if (i < p[k])
        {
            v[0][1].push_back({k, p[k] - i});
            u[0][i].push_back(k);
            if (p[k] < n)
            {
                v[1][i + 1].push_back({k, n - p[k]});
                u[1][n + i - p[k]].push_back(k);
            }
            v[0][n + i - p[k] + 1].push_back({k, 0});
        } else
        {
            v[1][1].push_back({k, i - p[k]});
            u[1][i - p[k]].push_back(k);
            v[0][i - p[k] + 1].push_back({k, 0});
            u[0][i].push_back(k);
            if (i < n) v[1][i + 1].push_back({k, n - p[k]});
        }
    }
    for (int i = 1; i <= n; i++) s[i] = n;
    for (int i = 1; i <= n; i++)
    {
        for (auto j : v[0][i]) q.push({i - j.second, j.first});
        if (!q.empty()) s[i] = min(s[i], i - q.top().first);
        for (auto j : u[0][i]) f[j] = true;
        while (!q.empty() && f[q.top().second]) q.pop();
    }
    while (!q.empty()) q.pop();
    for (int i = 1; i <= n; i++) f[i] = false;
    for (int i = 1; i <= n; i++)
    {
        for (auto j : v[1][i]) q.push({n + 1 - i - j.second, j.first});
        if (!q.empty()) s[i] = min(s[i], n + 1 - i - q.top().first);
        for (auto j : u[1][i]) f[j] = true;
        while (!q.empty() && f[q.top().second]) q.pop();
    }
    for (int i = 1; i <= n; i++) printf("%d\n", s[i]);
    return 0;
}


#include <cstdio>
#include <queue>
#include <vector>

using namespace std;

const int N = 1000010;
int n, d, a[N], b[N], p[N], s[N];
bool f[N];
vector<pair<int, int>> v[2][N];
vector<int> u[2][N];
priority_queue<pair<int, int>> q;

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
    for (int i = 1; i <= n; i++) p[a[i]] = i;
    for (int i = 1; i <= n; i++)
    {
        int k = b[i];
        if (i == p[k])
        {
            v[0][1].push_back({++d, 0});
            u[0][i].push_back(d);
            if (i < n) v[1][i + 1].push_back({++d, n - i});
        } else
        if (i < p[k])
        {
            v[0][1].push_back({++d, p[k] - i});
            u[0][i].push_back(d);
            if (p[k] < n)
            {
                v[1][i + 1].push_back({++d, n - p[k]});
                u[1][n + i - p[k]].push_back(d);
            }
            v[0][n + i - p[k] + 1].push_back({++d, 0});
        } else
        {
            v[1][1].push_back({++d, i - p[k]});
            u[1][i - p[k]].push_back(d);
            v[0][i - p[k] + 1].push_back({++d, 0});
            u[0][i].push_back(d);
            if (i < n) v[1][i + 1].push_back({++d, n - p[k]});
        }
    }
    for (int i = 1; i <= n; i++) s[i] = n;
    for (int i = 1; i <= n; i++)
    {
        for (auto j : v[0][i]) f[j.first] = false, q.push({i - j.second, j.first});
        if (!q.empty()) s[i] = min(s[i], i - q.top().first);
        for (auto j : u[0][i]) f[j] = true;
        while (!q.empty() && f[q.top().second]) q.pop();
    }
    while (!q.empty()) q.pop();
    for (int i = 1; i <= n; i++) f[i] = false;
    for (int i = 1; i <= n; i++)
    {
        for (auto j : v[1][i]) f[j.first] = false, q.push({n + 1 - i - j.second, j.first});
        if (!q.empty()) s[i] = min(s[i], n + 1 - i - q.top().first);
        for (auto j : u[1][i]) f[j] = true;
        while (!q.empty() && f[q.top().second]) q.pop();
    }
    for (int i = 1; i <= n; i++) printf("%d\n", s[i]);
    return 0;
}


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

int ai[maxn],bi[maxn],store[maxn];
multiset<int> ms;

void printms(){

	for(multiset<int>::iterator it = ms.begin();it != ms.end();++it) printf(" %d",*it);
	printf("\n");
}

int main(){
	int n;
	scanf("%d",&n);
	for(int i = 0;i < n;++i){
		int temp;
		scanf("%d",&temp);
		ai[temp] = i;
	}
    for(int i = 0;i < n;++i){
    	scanf("%d",&bi[i]);
    	ms.insert(i - ai[bi[i]]);
	}
	int ans,lft;
	for(int i = 0;i < n;++i){
		multiset<int>::iterator it = ms.lower_bound(i);
		multiset<int>::iterator temp = it;
		if(it == ms.end()) ans = i - *(--it);
		else if(it == ms.begin()) ans = (*it) - i;
		else ans = min(i - *(--temp),(*it) - i);
		printf("%d\n",ans);

		ms.erase(ms.lower_bound(i - ai[bi[i]]));
		ms.insert(i + n - ai[bi[i]]);
	}
	return 0;
}
/*
7
1 2 3 4 5 6 7
7 6 5 4 3 2 1
*/


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

int ai[maxn],bi[maxn],store[maxn];
multiset<int> ms;

void printms(){

	for(multiset<int>::iterator it = ms.begin();it != ms.end();++it) printf(" %d",*it);
	printf("\n");
}

int main(){
	int n;
	scanf("%d",&n);
	for(int i = 1;i <= n;++i){
		int temp;
		scanf("%d",&temp);
		ai[temp] = i;
	}
    for(int i = 1;i <= n;++i){
    	scanf("%d",&bi[i]);
    	ms.insert(i - ai[bi[i]]);
	}
	int ans,lft;
	for(int i = n,j = 0;i >= 1;--i,--j){
//		printms();
		multiset<int>::iterator it = ms.lower_bound(j);
		ans = (it == ms.end() ? maxn : (*it));
		lft = (it == ms.begin() ? maxn : *(--it));
		ans = min(abs(ans - j),abs(lft - j));
		printf("%d\n",ans);

		it = ms.lower_bound(i - ai[bi[i]]);
		ms.erase(it);
		ms.insert(1 - ai[bi[i]] + j - 1);
//		printf("i insert: %d\n",1 - ai[bi[i]] + j - 1);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;

int mp[maxn],bi[maxn],n;
multiset<int> st;

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


#include <bits/stdc++.h>
using namespace std;
#define DBG(x) cout << #x << " = " << x << endl;
/*
Compile:
    -Wall -Wextra -pedantic -std=c++11 -O2 -Wshadow -Wformat=2 -Wfloat-equal -Wconversion -Wlogical-op -Wcast-qual -Wcast-align -fwhole-program -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -lmcheck -fsanitize=address

Check for memory usage:
    valgrind --tool=massif
 */

const int INF = 1e9;

class SegmentTree
{
    // Segment tree from http://codeforces.com/blog/entry/18051
    vector<int> t, d;
    int n, h;

void apply(int p, int value) {
  t[p] += value;
  if (p < n) d[p] += value;
}

void build(int p) {
  while (p > 1) p >>= 1, t[p] = min(t[p<<1], t[p<<1|1]) + d[p];
}

void push(int p) {
  for (int s = h; s > 0; --s) {
    int i = p >> s;
    if (d[i] != 0) {
      apply(i<<1, d[i]);
      apply(i<<1|1, d[i]);
      d[i] = 0;
    }
  }
}

void inc(int l, int r, int value) {
  l += n, r += n;
  int l0 = l, r0 = r;
  for (; l < r; l >>= 1, r >>= 1) {
    if (l&1) apply(l++, value);
    if (r&1) apply(--r, value);
  }
  build(l0);
  build(r0 - 1);
}

int query(int l, int r) {
  l += n, r += n;
  push(l);
  push(r - 1);
  int res = 2e9;
  for (; l < r; l >>= 1, r >>= 1) {
    if (l&1) res = min(res, t[l++]);
    if (r&1) res = min(t[--r], res);
  }
  return res;
}
public:
    void init(int _n)
    {
        n = _n;
        h = sizeof(int) * 8 - __builtin_clz(n);
        t.resize(2 * n + 2);
        for(int i = 0; i < 2 * n + 2; i++)
            t[i] = INF;
        d.resize(n + 2);
        for(int i = 0; i < n + 2; i++)
            d[i] = 0;
    }
    int get_min(int le, int rg)
    {
        return query(le - 1, rg);
    }
    void add(int le, int rg, int val)
    {
        inc(le - 1, rg, val);
    }

    void erase(int pos)
    {
        add(pos, pos, -get_min(pos, pos));
        add(pos, pos, INF);
    }

    void insert(int pos, int val)
    {
        add(pos, pos, -get_min(pos, pos));
        add(pos, pos, val);
    }
} tree_grow, tree_shrink;

const int N = 100500;

unordered_set<int> zero[N];
int a[N], b[N], pos_in_a[N];
int answer[N];
int n;

inline int moves_to_eq(int val, int pos)
{
    int real_pos = pos_in_a[val];
    if(pos >= real_pos)
        return pos - real_pos;
    return pos + n - real_pos;
}

inline int dist(int val, int pos)
{
    return abs(pos - pos_in_a[val]);
}

void add_somewhere(int val, int pos)
{
    if(pos == 0) {
        tree_grow.insert(val, dist(val, pos));
    } else {
        if(dist(val, pos - 1) < dist(val, pos)) {
            tree_shrink.insert(val, dist(val, pos));
        } else {
            tree_grow.insert(val, dist(val, pos));
        }
    }
}

void print_tree(SegmentTree &t)
{
    for(int i = 1; i <= n; i++)
        cout << t.get_min(i, i) << " ";
    cout << endl;
}

int main()
{
    cin.sync_with_stdio(false);
    cin >> n;

    tree_grow.init(n);
    tree_shrink.init(n);

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

    for(int j = 0; j < n; j++) {
        cin >> b[j];
        zero[moves_to_eq(b[j], j)].insert(b[j]);
        add_somewhere(b[j], j);
    }

    for(int i = 0; i < n; i++) {
        answer[i] = min(tree_grow.get_min(1, n), tree_shrink.get_min(1, n));
        tree_grow.erase(b[i]);
        tree_shrink.erase(b[i]);
        tree_shrink.insert(b[i], n - pos_in_a[b[i]]);
        for(const auto &u: zero[i]) {
            if(u == b[i])
                continue;
            tree_shrink.erase(u);
            tree_grow.insert(u, 0);
        }

        tree_shrink.add(1, n, -1);
        tree_grow.add(1, n, 1);

    }

    for(int i = 0; i < n; i++)
        cout << answer[i] << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define Nmax 1000005
#define pb push_back
#define mp make_pair
#define INF 2000000000
#define eps 0.000000000001

using namespace std;

int n;
int a[Nmax],b[Nmax],pa[Nmax],pb[Nmax],Left[Nmax],Right[Nmax],Tip[Nmax],Val[Nmax],aux0[Nmax],aux1[Nmax],l1,l0;

struct el
{
    int timp,ind;
    bool operator <(const el A) const
    {
        return timp<A.timp;
    }
};
el events[Nmax];
int l,fr[Nmax];
multiset <int> T1,T0;

int main()
{
    int i,j,p,len=0;
    el w;
    #ifndef ONLINE_JUDGE
        freopen ("date.in","r",stdin);
        freopen ("date.out","w",stdout);
    #endif
    cin.sync_with_stdio(0);
    cin>>n;
    for(i=1;i<=n;++i)
    {
        cin>>a[i]; pa[a[i]]=i;
    }
    for(i=1;i<=n;++i)
    {
        cin>>b[i]; pb[b[i]]=i;
    }
    for(i=1;i<=n;++i)
        if(pb[i]<=pa[i])
        {
            ++l;
            Tip[l]=1; Left[l]=1; Right[l]=pa[i]-pb[i]+1; Val[l]=pa[i]-pb[i];
            ++l;
            Tip[l]=0; Left[l]=pa[i]-pb[i]+1; Right[l]=n-pb[i]+1; Val[l]=0;
            ++l;
            Tip[l]=1; Left[l]=n-pb[i]+2; Right[l]=n; Val[l]=pa[i]-1;
        }
        else
        {
            ++l;
            Tip[l]=0; Left[l]=1; Right[l]=n-pb[i]+1; Val[l]=pb[i]-pa[i];
            ++l;
            Tip[l]=1; Left[l]=n-pb[i]+2; Right[l]=n-pb[i]+1+pa[i]; Val[l]=pa[i]-1;
            ++l;
            Tip[l]=0; Left[l]=n-pb[i]+1+pa[i]; Right[l]=n; Val[l]=0;
        }
    for(i=1;i<=l;++i)
    {

        //cout<<Left[i]<<" "<<Right[i]<<" "<<Tip[i]<<" "<<Val[i]<<"\n";

        w.timp=Left[i]; w.ind=i;
        events[++len]=w;
        w.timp=Right[i]; w.ind=i;
        events[++len]=w;
    }
    sort(events+1,events+len+1);
    for(i=1,p=1;i<=n;++i)
    {
        l0=l1=0;
        for(;p<=len && events[p].timp==i;++p)
        {
            int x=events[p].ind;
            ++fr[x];
            if(fr[x]==1)
            {
                if(Tip[x]) T1.insert(Val[x]+Left[x]);
                else T0.insert(Val[x]-Left[x]);
            }
            else
            {
                if(Tip[x]) aux1[++l1]=Val[x]+Left[x];
                else aux0[++l0]=Val[x]-Left[x];
            }
        }
        int sol=INF;
        if(!T1.empty()) sol=min(sol,*T1.begin()-i);
        if(!T0.empty()) sol=min(sol,*T0.begin()+i);
        cout<<sol<<"\n";
        for(j=1;j<=l0;++j) T0.erase(T0.find(aux0[j]));
        for(j=1;j<=l1;++j) T1.erase(T1.find(aux1[j]));
    }
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define Nmax 1000005
#define pb push_back
#define mp make_pair
#define INF 2000000000
#define eps 0.000000000001

using namespace std;

int n;
int a[Nmax],b[Nmax],pa[Nmax],pb[Nmax],Left[Nmax],Right[Nmax],Tip[Nmax],Val[Nmax],aux0[Nmax],aux1[Nmax],l1,l0;

struct el
{
    int timp,ind;
    bool operator <(const el A) const
    {
        return timp<A.timp;
    }
};
el events[Nmax];
int l,fr[Nmax];
multiset <int> T1,T0;

int main()
{
    int i,j,p,len=0;
    el w;
    #ifndef ONLINE_JUDGE
        freopen ("date.in","r",stdin);
        freopen ("date.out","w",stdout);
    #endif
    cin.sync_with_stdio(0);
    cin>>n;
    for(i=1;i<=n;++i) cin>>a[i];
    reverse(a+1,a+n+1);
    for(i=1;i<=n;++i)
    {
        pa[a[i]]=i;
    }
    for(i=1;i<=n;++i) cin>>b[i];
    reverse(b+1,b+n+1);
    for(i=1;i<=n;++i)
    {
        pb[b[i]]=i;
    }
    for(i=1;i<=n;++i)
        if(pb[i]<=pa[i])
        {
            ++l;
            Tip[l]=1; Left[l]=1; Right[l]=pa[i]-pb[i]+1; Val[l]=pa[i]-pb[i];
            ++l;
            Tip[l]=0; Left[l]=pa[i]-pb[i]+1; Right[l]=n-pb[i]+1; Val[l]=0;
            ++l;
            Tip[l]=1; Left[l]=n-pb[i]+2; Right[l]=n; Val[l]=pa[i]-1;
        }
        else
        {
            ++l;
            Tip[l]=0; Left[l]=1; Right[l]=n-pb[i]+1; Val[l]=pb[i]-pa[i];
            ++l;
            Tip[l]=1; Left[l]=n-pb[i]+2; Right[l]=n-pb[i]+1+pa[i]; Val[l]=pa[i]-1;
            ++l;
            Tip[l]=0; Left[l]=n-pb[i]+1+pa[i]; Right[l]=n; Val[l]=0;
        }
    for(i=1;i<=l;++i)
    {

        //cout<<Left[i]<<" "<<Right[i]<<" "<<Tip[i]<<" "<<Val[i]<<"\n";

        w.timp=Left[i]; w.ind=i;
        events[++len]=w;
        w.timp=Right[i]; w.ind=i;
        events[++len]=w;
    }
    sort(events+1,events+len+1);
    for(i=1,p=1;i<=n;++i)
    {
        l0=l1=0;
        for(;p<=len && events[p].timp==i;++p)
        {
            int x=events[p].ind;
            ++fr[x];
            if(fr[x]==1)
            {
                if(Tip[x]) T1.insert(Val[x]+Left[x]);
                else T0.insert(Val[x]-Left[x]);
            }
            else
            {
                if(Tip[x]) aux1[++l1]=Val[x]+Left[x];
                else aux0[++l0]=Val[x]-Left[x];
            }
        }
        int sol=INF;
        if(!T1.empty()) sol=min(sol,*T1.begin()-i);
        if(!T0.empty()) sol=min(sol,*T0.begin()+i);
        cout<<sol<<"\n";
        for(j=1;j<=l0;++j) T0.erase(T0.find(aux0[j]));
        for(j=1;j<=l1;++j) T1.erase(T1.find(aux1[j]));
    }
    return 0;
}


//hoax submission
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define pb push_back
#define f first
#define s second
#define mod 1000000007
#define inf 1e8

#define pi pair<ll,ll>
#define pii pair<pi,ll>
#define f first
#define mp make_pair
#define pb push_back
#define s second
#define rep(i,n) for(int i=0;i<n;i++)
#define forup(i,a,b) for(int i=a;i<=b;i++)
int pos1[100011];
int pos2[100011];
int val[100011];
int main(){
	int n;
	cin >> n;
	int a[n],b[2*n];
	rep(i,n)
        cin >> a[i] , a[i]-- , pos1[a[i]]=i;
    rep(i,n)
        cin >> b[i] , b[i]-- , pos2[b[i]]=i;
    set<pi>p1,p2;
    for(int i=0;i<n;i++){
        if(pos1[a[i]]>=pos2[a[i]]){
            p1.insert(make_pair(abs(pos1[a[i]]-pos2[a[i]]),a[i]));
            val[a[i]]=abs(pos1[a[i]]-pos2[a[i]]);
        }
        else{
            p2.insert(make_pair(abs(pos1[a[i]]-pos2[a[i]]),a[i]));
        }
    }
    int ans;
    for(int s=0;s<n;s++){

        while(p2.size()>0 and (*p2.begin()).first-s<=0){
            pi r=*p2.begin();
            p2.erase(r);
            r.f=-s;
            val[r.s]=r.f;
            p1.insert(r);
        }

        ans=n+2;
        if(p1.size())
        ans=min(ans,p1.begin()->first+s);
        if(p2.size())
        ans=min(ans,p2.begin()->first-s);
        cout<<ans<<"\n";

        pos2[b[s]]=n-1;

        p2.insert(make_pair(s+1+abs(pos1[b[s]]-pos2[b[s]]),b[s]));
        p1.erase(make_pair( val[b[s]] , b[s] ));

    }
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 1050005
#define mod 10007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//**************************************************************************************

int a[maxn];
int b[maxn];
multiset<int> s;
multiset<int>::iterator it;
int ans;
int main()
{
    int n=read();
    for(int i=0;i<n;i++)
    {
        int x=read();
        a[x]=i;
    }
    for(int i=0;i<n;i++)
    {
        b[i]=read();
        s.insert(i-a[b[i]]);
    }
    for(int i=0;i<n;i++)
    {
        ans=inf;
        it=s.lower_bound(i);
        if(it!=s.end())
            ans=min(ans,*it-i);
        if(it!=s.begin())
            ans=min(ans,i-*(--it));
        printf("%d\n",ans);
        it=s.find(i-a[b[i]]);
        s.erase(it);
        s.insert(i-a[b[i]]+n);
    }
}


#include<bits/stdc++.h>
#define ii          pair<int,int>
#define vi          vector<int>
#define x           first
#define y           second
using namespace std;
 struct SEG{
     int comb,n;
     vector<ii> st;
     SEG(int n){
         this->n = n;
         comb = 1;
         for(int i=n-1;i;i/=2) comb *= 2;
         st.resize(2*comb,{n,1});
     }
     void update(int cur, int l, int r, int rl, int rr, ii state){
        if(l > rr || r < rl) return;
        int mid = (rl + rr) / 2;
        if(l <= rl && r >= rr){
            if(cur >= comb || st[cur].y == state.y){
                st[cur].x = min(st[cur].x, state.x);
                return;
            }
            if(st[cur].y == -1) swap(st[cur],state);
            if(st[cur].x + mid - rl < state.x - (mid - rl)){
                update(cur * 2 + 1,l,r,mid + 1, rr, {state.x - (mid - rl +1), state.y});
                return;
            }
            swap(st[cur],state);
            update(cur * 2, l, r, rl, mid,state);
            return;
        }
         update(cur * 2, l, r, rl, mid,state);
         update(cur * 2 + 1,max(l, mid + 1),r,mid + 1, rr, {state.x + state.y * max(mid + 1 - l,(int)0), state.y});
     }
     int query(int cur,int ind, int rl, int rr){
         int mid = (rl + rr)/2;
         int val = st[ind].x + st[ind].y * (cur - rl);
         if(ind >= comb) return val;
         if(cur <= mid) return min(val, query(cur, ind * 2, rl, mid));
         else return min(val, query(cur, ind * 2 + 1 , mid + 1, rr));
     }
     int update(int l, int r, ii state){
         if(r < l){
             update(1,l,n-1, 0, comb - 1, state);
             update(1,0,r, 0, comb - 1, {state.x + state.y*(n-l), state.y});
         }
         else update(1,l,r, 0, comb - 1, state);
     }
 };

int32_t main()
{
    ios::sync_with_stdio(false);
    int n; cin >> n;
    SEG seg(n);
    vi a(n),b(n);
    for(int i=0,tmp;i<n;i++) cin >> tmp, --tmp, a[tmp] = i;
    for(int i=0,tmp;i<n;i++) cin >> tmp, --tmp, b[i] = a[tmp];
    for(int i=0;i<n;i++){
        int dist = (b[i] - i + n) % n, in = (n - i) % n;
        seg.update(in, dist % n, {b[i], -1});
        if(b[i]!=n-1) seg.update((dist + 1) % n, (in - 1 + n) % n,{1, 1});
    }
    for(int i=n;i>0;i--) cout<<seg.query(i%n, 1 ,0 ,seg.comb - 1)<<endl;
    return 0;
}
//hello


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <numeric>
#include <deque>
#include <utility>
#include <bitset>
#include <iostream>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;

int ret[100005];
int pos1[100005], pos2[100005], a[100005], n;

int dist(int x, int y){
	return abs(y - x);
}

int main(){
	memset(ret, 0x3f, sizeof(ret));
	scanf("%d",&n);
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos1[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos2[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		int dx = (pos2[i] - pos1[i] + n) % n;
		for(int j=dx; j<dx+n; j++){
			if(j >= n) ret[j%n] = min(ret[j%n], (dx - j + n) % n);
			else ret[j%n] = min(ret[j%n], (n - dx + j) % n);
		}
	}
	for(int i=0; i<n; i++){
		printf("%d\n",ret[i]);
	}
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <numeric>
#include <deque>
#include <utility>
#include <bitset>
#include <iostream>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;

int ret[100005];
int pos1[100005], pos2[100005], n;
int lg[100005], rg[100005];

int main(){
	scanf("%d",&n);
	fill(lg, lg + n, -1e9);
	fill(rg, rg + n, 1e9);
	fill(ret, ret + 1 + n, 1e9);
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos1[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos2[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		for(int j=0; j<=pos2[i] - pos1[i]; j++){
			rg[j] = min(rg[j], pos2[i] - pos1[i]);
		}
		for(int j=max(pos2[i] - pos1[i] + 1, 0); j<=pos2[i]; j++){
			lg[j] = max(lg[j], pos2[i] - pos1[i]);
		}
		for(int j=pos2[i]+1; j<min(pos2[i] - pos1[i] + n + 1, n); j++){
			lg[j] = max(rg[j], pos2[i] - pos1[i] + n);
		}
		for(int j=min(pos2[i] - pos1[i] + n + 1, n); j<n; j++){
			rg[j] = min(lg[j], pos2[i] - pos1[i] + n);
		}
		for(int j=0; j<=pos2[i]; j++){
			// [0, p2 - p1] ~ (p2-p1, p2)
			ret[j] = min(ret[j], abs(pos2[i] - pos1[i] - j));
		}
		for(int j=pos2[i]+1; j<n; j++){
			// [p2, p2 - p1 + n] / (p2 - p1 + n, n-1)
			ret[j] = min(ret[j], abs(pos2[i] - pos1[i] - j + n));
		}
	}
	for(int i=0; i<n; i++){
		printf("%d\n",ret[i]);
	}
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <numeric>
#include <deque>
#include <utility>
#include <bitset>
#include <iostream>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;

int ret[100005];
int pos1[100005], pos2[100005], n;

int dist(int x, int y){
	return abs(y - x);
}

int main(){
	memset(ret, 0x3f, sizeof(ret));
	scanf("%d",&n);
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos1[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos2[t] = i-1;
	}
	for(int i=0; i<n; i++){
		for(int j=1; j<=n; j++){
			ret[i] = min(ret[i], dist(pos1[j], (pos2[j] + i)%n));
			// pos2[i] - pos1[i]
		}
	}
	for(int i=0; i<n; i++){
		printf("%d\n",ret[i]);
	}
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <numeric>
#include <deque>
#include <utility>
#include <bitset>
#include <iostream>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;

int ret[100005];
int pos1[100005], pos2[100005], n;
int lg[100005], rg[100005];

int main(){
	scanf("%d",&n);
	fill(lg, lg + n, -1e9);
	fill(rg, rg + n, 1e9);
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos1[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos2[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		for(int j=0; j<=pos2[i] - pos1[i]; j++){
			rg[j] = min(rg[j], pos2[i] - pos1[i]);
		}
		for(int j=max(pos2[i] - pos1[i] + 1, 0); j<pos2[i]; j++){
			lg[j] = max(lg[j], pos2[i] - pos1[i]);
		}
		for(int j=pos2[i]; j<min(pos2[i] - pos1[i] + n + 1, n); j++){
			lg[j] = max(rg[j], pos2[i] - pos1[i] + n);
		}
		for(int j=min(pos2[i] - pos1[i] + n + 1, n); j<n; j++){
			rg[j] = min(lg[j], pos2[i] - pos1[i] + n);
		}
		for(int j=0; j<pos2[i]; j++){
			// [0, p2 - p1] ~ (p2-p1, p2)
			ret[j] = min(ret[j], abs(pos2[i] - pos1[i] - j));
		}
		for(int j=pos2[i]; j<n; j++){
			// [p2, p2 - p1 + n] / (p2 - p1 + n, n-1)
			ret[j] = min(ret[j], abs(pos2[i] - pos1[i] - j + n));
		}
	}
	for(int i=0; i<n; i++){
		printf("%d\n",min(i - lg[i], rg[i] - i));
	}
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <numeric>
#include <deque>
#include <utility>
#include <bitset>
#include <iostream>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;

int ret[100005];
int pos1[100005], pos2[100005], n;
int lg[100005], rg[100005];

int main(){
	scanf("%d",&n);
	fill(lg, lg + n, -1e9);
	fill(rg, rg + n, 1e9);
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos1[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos2[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		for(int j=0; j<=pos2[i] - pos1[i]; j++){
			rg[j] = min(rg[j], pos2[i] - pos1[i]);
		}
		for(int j=max(pos2[i] - pos1[i] + 1, 0); j<pos2[i]; j++){
			lg[j] = max(lg[j], pos2[i] - pos1[i]);
		}
		for(int j=pos2[i]; j<min(pos2[i] - pos1[i] + n + 1, n); j++){
			lg[j] = max(rg[j], pos2[i] - pos1[i] + n);
		}
		for(int j=min(pos2[i] - pos1[i] + n + 1, n); j<n; j++){
			rg[j] = min(lg[j], pos2[i] - pos1[i] + n);
		}
		for(int j=0; j<pos2[i]; j++){
			// [0, p2 - p1] ~ (p2-p1, p2)
			ret[j] = min(ret[j], abs(pos2[i] - pos1[i] - j));
		}
		for(int j=pos2[i]; j<n; j++){
			// [p2, p2 - p1 + n] / (p2 - p1 + n, n-1)
			ret[j] = min(ret[j], abs(pos2[i] - pos1[i] - j + n));
		}
	}
	for(int i=0; i<n; i++){
		printf("%d\n",ret[i]);
	}
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <numeric>
#include <deque>
#include <utility>
#include <bitset>
#include <iostream>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;

int ret[100005];
int pos1[100005], pos2[100005], n;
int lg[100005], rg[100005];

int main(){
	scanf("%d",&n);
	fill(lg, lg + n, -1e9);
	fill(rg, rg + n, 1e9);
	fill(ret, ret + 1 + n, 1e9);
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos1[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos2[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		for(int j=0; j<=pos2[i] - pos1[i]; j++){
			rg[j] = min(rg[j], pos2[i] - pos1[i]);
		}
		for(int j=max(pos2[i] - pos1[i] + 1, 0); j<pos2[i]; j++){
			lg[j] = max(lg[j], pos2[i] - pos1[i]);
		}
		for(int j=pos2[i]; j<min(pos2[i] - pos1[i] + n + 1, n); j++){
			lg[j] = max(rg[j], pos2[i] - pos1[i] + n);
		}
		for(int j=min(pos2[i] - pos1[i] + n + 1, n); j<n; j++){
			rg[j] = min(lg[j], pos2[i] - pos1[i] + n);
		}
		for(int j=0; j<pos2[i]; j++){
			// [0, p2 - p1] ~ (p2-p1, p2)
			ret[j] = min(ret[j], abs(pos2[i] - pos1[i] - j));
		}
		for(int j=pos2[i]; j<n; j++){
			// [p2, p2 - p1 + n] / (p2 - p1 + n, n-1)
			ret[j] = min(ret[j], abs(pos2[i] - pos1[i] - j + n));
		}
	}
	for(int i=0; i<n; i++){
		printf("%d\n",ret[i]);
	}
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <numeric>
#include <deque>
#include <utility>
#include <bitset>
#include <iostream>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;

int ret[100005];
int pos1[100005], pos2[100005], n;

int dist(int x, int y){
	return min(abs(y - x), n - abs(y - x));
}

int main(){
	memset(ret, 0x3f, sizeof(ret));
	scanf("%d",&n);
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos1[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos2[t] = i-1;
	}
	for(int i=0; i<n; i++){
		for(int j=1; j<=n; j++){
			ret[i] = min(ret[i], dist(pos1[j], (pos2[j] + i)%n));
			// pos2[i] - pos1[i]
		}
	}
	for(int i=0; i<n; i++){
		printf("%d\n",ret[i]);
	}
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <numeric>
#include <deque>
#include <utility>
#include <bitset>
#include <iostream>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;

int ret[100005];
int pos1[100005], pos2[100005], n;
int lg[100005], rg[100005];

int main(){
	scanf("%d",&n);
	fill(lg, lg + n, -1e9);
	fill(rg, rg + n, 1e9);
	fill(ret, ret + 1 + n, 1e9);
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos1[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos2[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		for(int j=0; j<=pos2[i] - pos1[i]; j++){
			rg[j] = min(rg[j], pos2[i] - pos1[i]);
		}
		for(int j=max(pos2[i] - pos1[i] + 1, 0); j<=pos2[i]; j++){
			lg[j] = max(lg[j], pos2[i] - pos1[i]);
		}
		for(int j=pos2[i]+1; j<min(pos2[i] - pos1[i] + n + 1, n); j++){
			rg[j] = min(rg[j], pos2[i] - pos1[i] + n);
		}
		for(int j=min(pos2[i] - pos1[i] + n + 1, n); j<n; j++){
			lg[j] = max(lg[j], pos2[i] - pos1[i] + n);
		}
		for(int j=0; j<=pos2[i]; j++){
			// [0, p2 - p1] ~ (p2-p1, p2)
			ret[j] = min(ret[j], abs(pos2[i] - pos1[i] - j));
		}
		for(int j=pos2[i]+1; j<n; j++){
			// [p2, p2 - p1 + n] / (p2 - p1 + n, n-1)
			ret[j] = min(ret[j], abs(pos2[i] - pos1[i] - j + n));
		}
	}
	for(int i=0; i<n; i++){
		printf("%d\n",min(i - lg[i], rg[i] - i));
	}
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <numeric>
#include <deque>
#include <utility>
#include <bitset>
#include <iostream>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;

int ret[100005];
int pos1[100005], pos2[100005], n;

int dist(int x, int y){
	return abs(y - x);
}

int main(){
	memset(ret, 0x3f, sizeof(ret));
	scanf("%d",&n);
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos1[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos2[t] = i-1;
	}
	for(int i=0; i<n; i++){
		for(int j=1; j<=n; j++){
			ret[i] = min(ret[i], dist(pos1[j], (pos2[j] + n - i)%n));
			// pos2[i] - pos1[i]
		}
	}
	for(int i=0; i<n; i++){
		printf("%d\n",ret[i]);
	}
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <numeric>
#include <deque>
#include <utility>
#include <bitset>
#include <iostream>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;

int ret[100005];
int pos1[100005], pos2[100005], n;
int lg[100005], rg[100005];

struct rmq{
	int tree[270000], lim;
	void init(int n){
		memset(tree, 0x3f, sizeof(tree));
		for(lim = 1; lim <= n; lim <<= 1);
	}
	void add(int s, int e, int x){
		s += lim;
		e += lim;
		while(s < e){
			if(s%2 == 1) tree[s] = min(tree[s], x);
			if(e%2 == 0) tree[e] = min(tree[e], x);
			s = (s+1)/2;
			e = (e-1)/2;
		}
		if(s == e) tree[s] = min(tree[s], x);
	}
	int query(int x){
		x += lim;
		int ret = 1e9;
		while(x){
			ret = min(ret, tree[x]);
			x >>= 1;
		}
		return ret;
	}
}rmq1, rmq2;

int main(){
	scanf("%d",&n);
	fill(lg, lg + n, -1e9);
	fill(rg, rg + n, 1e9);
	fill(ret, ret + 1 + n, 1e9);
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos1[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos2[t] = i-1;
	}
	rmq1.init(n);
	rmq2.init(n);
	for(int i=1; i<=n; i++){
		rmq2.add(0, pos2[i] - pos1[i], pos2[i] - pos1[i]);
		rmq2.add(pos2[i] + 1, min(pos2[i] - pos1[i] + n, n-1), pos2[i] - pos1[i] + n);
		rmq1.add(max(pos2[i] - pos1[i] + 1, 0), pos2[i], pos1[i] - pos2[i]);
		rmq1.add(min(pos2[i] - pos1[i] + n + 1, n), n-1, pos1[i] - pos2[i] - n);
	}
	for(int i=0; i<n; i++){
		printf("%d\n",min(i + rmq1.query(i), rmq2.query(i) - i));
	}
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <numeric>
#include <deque>
#include <utility>
#include <bitset>
#include <iostream>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;

int ret[100005];
int pos1[100005], pos2[100005], n;
int lg[100005], rg[100005];

int main(){
	scanf("%d",&n);
	fill(lg, lg + n, -1e9);
	fill(rg, rg + n, 1e9);
	fill(ret, ret + 1 + n, 1e9);
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos1[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos2[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		for(int j=0; j<=pos2[i] - pos1[i]; j++){
			rg[j] = min(rg[j], pos2[i] - pos1[i]);
		}
		for(int j=max(pos2[i] - pos1[i] + 1, 0); j<pos2[i]; j++){
			lg[j] = max(lg[j], pos2[i] - pos1[i]);
		}
		for(int j=pos2[i]; j<min(pos2[i] - pos1[i] + n + 1, n); j++){
			lg[j] = max(rg[j], pos2[i] - pos1[i] + n);
		}
		for(int j=min(pos2[i] - pos1[i] + n + 1, n); j<n; j++){
			rg[j] = min(lg[j], pos2[i] - pos1[i] + n);
		}
		for(int j=0; j<=pos2[i]; j++){
			// [0, p2 - p1] ~ (p2-p1, p2)
			ret[j] = min(ret[j], abs(pos2[i] - pos1[i] - j));
		}
		for(int j=pos2[i]+1; j<n; j++){
			// [p2, p2 - p1 + n] / (p2 - p1 + n, n-1)
			ret[j] = min(ret[j], abs(pos2[i] - pos1[i] - j + n));
		}
	}
	for(int i=0; i<n; i++){
		printf("%d\n",ret[i]);
	}
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <numeric>
#include <deque>
#include <utility>
#include <bitset>
#include <iostream>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;

int pos1[100005], pos2[100005], n;

struct rmq{
	int tree[270000], lim;
	void init(int n){
		memset(tree, 0x3f, sizeof(tree));
		for(lim = 1; lim <= n; lim <<= 1);
	}
	void add(int s, int e, int x){
		s += lim;
		e += lim;
		while(s < e){
			if(s%2 == 1) tree[s] = min(tree[s], x);
			if(e%2 == 0) tree[e] = min(tree[e], x);
			s = (s+1)/2;
			e = (e-1)/2;
		}
		if(s == e) tree[s] = min(tree[s], x);
	}
	int query(int x){
		x += lim;
		int ret = 1e9;
		while(x){
			ret = min(ret, tree[x]);
			x >>= 1;
		}
		return ret;
	}
}rmq1, rmq2;

int main(){
	scanf("%d",&n);
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos1[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos2[t] = i-1;
	}
	rmq1.init(n);
	rmq2.init(n);
	for(int i=1; i<=n; i++){
		rmq2.add(0, pos2[i] - pos1[i], pos2[i] - pos1[i]);
		rmq2.add(pos2[i] + 1, min(pos2[i] - pos1[i] + n, n-1), pos2[i] - pos1[i] + n);
		rmq1.add(max(pos2[i] - pos1[i] + 1, 0), pos2[i], pos1[i] - pos2[i]);
		rmq1.add(min(pos2[i] - pos1[i] + n + 1, n), n-1, pos1[i] - pos2[i] - n);
	}
	for(int i=0; i<n; i++){
		printf("%d\n",min(i + rmq1.query(i), rmq2.query(i) - i));
	}
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <numeric>
#include <deque>
#include <utility>
#include <bitset>
#include <iostream>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;

int ret[100005];
int pos1[100005], pos2[100005], n;
int lg[100005], rg[100005];

int main(){
	scanf("%d",&n);
	fill(lg, lg + n, -1e9);
	fill(rg, rg + n, 1e9);
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos1[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		int t;
		scanf("%d",&t);
		pos2[t] = i-1;
	}
	for(int i=1; i<=n; i++){
		for(int j=0; j<=pos2[i] - pos1[i]; j++){
			rg[j] = min(rg[j], pos2[i] - pos1[i]);
		}
		for(int j=max(pos2[i] - pos1[i] + 1, 0); j<pos2[i]; j++){
			lg[j] = max(lg[j], pos2[i] - pos1[i]);
		}
		for(int j=pos2[i]; j<min(pos2[i] - pos1[i] + n + 1, n); j++){
			rg[j] = min(rg[j], pos2[i] - pos1[i] + n);
		}
		for(int j=min(pos2[i] - pos1[i] + n + 1, n); j<n; j++){
			lg[j] = max(lg[j], pos2[i] - pos1[i] + n);
		}
		for(int j=0; j<pos2[i]; j++){
			// [0, p2 - p1] ~ (p2-p1, p2)
			ret[j] = min(ret[j], abs(pos2[i] - pos1[i] - j));
		}
		for(int j=pos2[i]; j<n; j++){
			// [p2, p2 - p1 + n] / (p2 - p1 + n, n-1)
			ret[j] = min(ret[j], abs(pos2[i] - pos1[i] - j + n));
		}
	}
	for(int i=0; i<n; i++){
		printf("%d\n",min(i - lg[i], rg[i] - i));
	}
}


#include<bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, A[N], B[N], rev[N];
multiset < int > S;
void Del(int val)
{
    auto it = S.lower_bound(val);
    if ((*it) == val) S.erase(it);
}
int Get(int base)
{
    int Mn = N;
    auto it = S.lower_bound(base);
    if (it != S.end())
        Mn = min(Mn, (*it) - base);
    if (it != S.begin())
        it --, Mn = min(Mn, base - (*it));
    return (Mn);
}
int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        scanf("%d", &A[i]), rev[A[i]] = i;
    for (int i = 0; i < n; i++)
        scanf("%d", &B[i]), S.insert(i - rev[B[i]]);
    for (int i = 0; i < n; i++)
    {
        printf("%d\n", Get(i));
        Del(i - rev[B[i]]);
        S.insert(n - 1 - rev[B[i]] + i + 1);
    }
    return (0);
}


#include <algorithm>
#include <deque>
#include <ios>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <array>
#include <random>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <iomanip>

//In The Need Of GOD
//A_Z ;)

using namespace std;
typedef long long ll;
#define mp make_pair
#define F first
#define S second
#define err(q) cout << #q << " = " << q;

const int N = 1e5 + 50, INF = 1e9;

int a[N], b[N], p[N], n;
multiset <int> s;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL), cout.tie(NULL);

	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> a[i];
		p[a[i]] = i;
	}
	for(int i = 0; i < n; i++) cin >> b[i];

	for(int i = 0; i < n; i++){
		int q = p[b[i]] - i;
		s.insert(q);
	}

	for(int i = 0; i < n; i++){
		int tmp, tmp2, tmp3;
		auto it = s.lower_bound(-1 * i);
		auto it2 = it, it3 = it;
		if(it2 != s.begin()) it2 --;
		if(it3 != s.end()) it3 ++;
		if(it3 == s.end()) it3 --;
		tmp = *it, tmp2 = *it2, tmp3 = *it3;
		tmp = abs(-1 * i - tmp), tmp2 = abs(-1 * i - tmp2), tmp3 = abs(-1 * i - tmp3);
		tmp = min(tmp, min(tmp2, tmp3));
		cout << tmp << '\n';

		int q = p[b[i]] - i;
		s.erase(s.find(q));
		q = p[b[i]] - (n - 1) - i - 1;
		s.insert(q);
	}

	return 0;
}


#include <algorithm>
#include <deque>
#include <ios>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <array>
#include <random>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <iomanip>

//In The Need Of GOD
//A_Z ;)

using namespace std;
typedef long long ll;
#define mp make_pair
#define F first
#define S second
#define err(q) cout << #q << " = " << q;

const int N = 1e5 + 50, INF = 1e9;

int a[N], b[N], p[N], n;
multiset <int> s;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL), cout.tie(NULL);

	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> a[i];
		p[a[i]] = i;
	}
	for(int i = 0; i < n; i++) cin >> b[i];

	for(int i = 0; i < n; i++){
		int q = p[b[i]] - i;
		s.insert(q);
	}

	for(int i = 0; i < n; i++){
		int tmp, tmp2, tmp3;
		auto it = s.lower_bound(-1 * i);
		auto it2 = it, it3 = it;
		if(it2 != s.begin()) it2 --;
		if(it3 != s.end()) it3 ++;
		if(it3 == s.end()) it3 --;
		tmp = *it, tmp2 = *it2, tmp3 = *it3;
		tmp = abs(-1 * i - tmp), tmp2 = abs(-1 * i - tmp2), tmp3 = abs(-1 * i - tmp3);
		tmp = min(tmp, min(tmp2, tmp3));
		cout << tmp << '\n';

		int q = p[b[i]] - i;
		s.erase(s.find(q));
		q = p[b[i]] - (n - 1) + i + 1;
		s.insert(q);
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned int uint;
vector<int> v1,v2,aux,pos;
vector<int> sweep;

int main()
{
    int n;
    scanf("%d",&n);
    v1.resize(n);
    v2.resize(n);
    pos.resize(n);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&v1[i]);
        pos[v1[i]-1] = i;
    }
    for(int i=0; i<n; i++)
        scanf("%d",&v2[i]);

    for(uint i=1; i<v2.size(); i++)
        aux.push_back(v2[i]);
    for(uint i=0; i<v2.size(); i++)
        aux.push_back(v2[i]);

    for(uint i=0; i<aux.size(); i++)
        sweep.push_back(pos[aux[i]-1] - i);

    stack<int>ans;
    multiset<int>nonneg,neg;
    for(int i=0; i<n; i++)
        if(sweep[i] >= 0)
            nonneg.insert(sweep[i]);
        else
            neg.insert(sweep[i]);

    int mn = 1<<30;
    if(neg.size() )mn = abs(*neg.rbegin() );
    if(nonneg.size())mn = min(mn , *nonneg.begin());
    ans.push(mn);

    int len = 0;
    for(uint i=n; i<sweep.size(); i++)
    {
        if(nonneg.find(sweep[i-n]) != nonneg.end())
        {
            nonneg.erase(nonneg.find(sweep[i-n]));
        }
        else
        {
            neg.erase(neg.find(sweep[i-n]));
        }
        len++;
        neg.insert(sweep[i]);
        while(1)
        {
            if(neg.size() == 0)break;
            int val = *neg.rbegin();
            if(val + len >= 0)
            {
                nonneg.insert(val);
                neg.erase(neg.find(val));
            }
            else
            {
                break;
            }
        }
        int mn = 1<<30;
        if(neg.size() )mn = abs(*neg.rbegin() + len);
        if(nonneg.size())mn = min(mn , *nonneg.begin() + len );
        ans.push(mn);
    }

    while(ans.size())
    {
        cout<<ans.top()<<"\n";
        ans.pop();
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned int uint;
vector<int> v1,v2,aux,pos;
vector<int> sweep;

int main(){
	int n;
	scanf("%d",&n);
	v1.resize(n);
	v2.resize(n);
	pos.resize(n);
	for(int i=0;i<n;i++){
		scanf("%d",&v1[i]);
		pos[v1[i]-1] = i;
	}
	for(int i=0;i<n;i++)
		scanf("%d",&v2[i]);

	for(uint i=1;i<v2.size();i++)
		aux.push_back(v2[i]);
	for(uint i=0;i<v2.size();i++)
		aux.push_back(v2[i]);

	for(uint i=0;i<aux.size();i++)
		sweep.push_back(pos[aux[i]-1] - i);

	stack<int>ans;
	multiset<int>nonneg,neg;
	for(int i=0;i<n;i++)
		if(sweep[i] >= 0)
			nonneg.insert(sweep[i]);
		else
			neg.insert(sweep[i]);

	ans.push(min( (*nonneg.begin() ), abs(*neg.rbegin())));
	int len = 0;
	for(uint i=n;i<sweep.size();i++){
		if(nonneg.find(sweep[i-n]) != nonneg.end()){
			nonneg.erase(nonneg.find(sweep[i-n]));
		}else{
			neg.erase(neg.find(sweep[i-n]));
		}
		len++;
		neg.insert(sweep[i]);
		while(1){
			if(neg.size() == 0)break;
			int val = *neg.rbegin();
			if(val + len >= 0){
				nonneg.insert(val);
				neg.erase(neg.find(val));
			}else{
				break;
			}
		}
		int mn = 1<<30;
		if(neg.size() )mn = abs(*neg.rbegin() + len);
		if(nonneg.size())mn = min(mn , *nonneg.begin() + len );
		ans.push(mn);
	}

	while(ans.size()){
		cout<<ans.top()<<"\n";
		ans.pop();
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned int uint;
vector<int> v1,v2,aux,pos;
vector<int> sweep;

int main(){
	int n;
	scanf("%d",&n);
	v1.resize(n);
	v2.resize(n);
	pos.resize(n);
	for(int i=0;i<n;i++){
		scanf("%d",&v1[i]);
		pos[v1[i]-1] = i;
	}
	for(int i=0;i<n;i++)
		scanf("%d",&v2[i]);
	for(uint i=1;i<v2.size();i++)
		aux.push_back(v2[i]);
	for(uint i=0;i<v2.size();i++)
		aux.push_back(v2[i]);
	for(uint i=0;i<aux.size();i++){
		sweep.push_back(pos[aux[i]-1] - i);
	}
	multiset<int> cur;
	int len = 0;
	stack<int>ans;
	for(int i=0;i<n;i++){
		cur.insert(sweep[i]);
	}
	int mx = (*cur.rbegin());
	int mn = (*cur.begin());
	if(mx > 0)mx -= len;else mx += len;
	if(mn > 0)mn -= len;else mn += len;
	ans.push(min(abs(mx) ,abs(mn) ));
	for(uint i=n;i<sweep.size();i++){
		cur.erase(cur.find(sweep[i-n]));
		cur.insert(sweep[i]);
		len++;
		mx = (*cur.rbegin());
		mn = (*cur.begin());
		if(mx > 0)mx -= len;else mx += len;
		if(mn > 0)mn -= len;else mn += len;
		ans.push(min(abs(mx) ,abs(mn) ));
	}
	while(ans.size()){
		cout<<ans.top()<<"\n";
		ans.pop();
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned int uint;
vector<int> v1,v2,aux,pos;
vector<int> sweep;

int main()
{
    int n;
    scanf("%d",&n);
    v1.resize(n);
    v2.resize(n);
    pos.resize(n);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&v1[i]);
        pos[v1[i]-1] = i;
    }
    for(int i=0; i<n; i++)
        scanf("%d",&v2[i]);

    for(uint i=1; i<v2.size(); i++)
        aux.push_back(v2[i]);
    for(uint i=0; i<v2.size(); i++)
        aux.push_back(v2[i]);

    for(uint i=0; i<aux.size(); i++)
        sweep.push_back(pos[aux[i]-1] - i);

    stack<int>ans;
    multiset<int>nonneg,neg;
    for(int i=0; i<n; i++)
        if(sweep[i] >= 0)
            nonneg.insert(sweep[i]);
        else
            neg.insert(sweep[i]);

    int mn = 1<<30;
    if(neg.size() )mn = abs(*neg.rbegin() );
    if(nonneg.size())mn = min(mn , *nonneg.begin());
    ans.push(mn);

    int len = 0;
    for(uint i=n; i<sweep.size(); i++)
    {
        if(nonneg.find(sweep[i-n]) != nonneg.end())
        {
            nonneg.erase(nonneg.find(sweep[i-n]));
        }
        else
        {
            neg.erase(neg.find(sweep[i-n]));
        }
        len++;
        neg.insert(sweep[i]);
        while(1)
        {
            if(neg.size() == 0)break;
            int val = *neg.rbegin();
            if(val + len >= 0)
            {
                nonneg.insert(val);
                neg.erase(neg.find(val));
            }
            else
            {
                break;
            }
        }
        int mn = 1<<30;
        if(neg.size() )mn = abs(*neg.rbegin() + len);
        if(nonneg.size())mn = min(mn , *nonneg.begin() + len );
        ans.push(mn);
    }
    stack<int>tmp;
    if(ans.size())
    {
        cout<<ans.top()<<"\n";
        ans.pop();
    }
    while(ans.size())
    {
        tmp.push(ans.top());
        ans.pop();
    }
    while(tmp.size())
    {
        cout<<tmp.top()<<"\n";
        tmp.pop();
    }
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1<<17;

int posA[MAXN];
int posB[MAXN];
int b[MAXN];

multiset<int> dists;

int main()
{
	ios_base::sync_with_stdio(false);

	int n, i;
	int x, sol;

	cin>>n;
	for (i = 0; i < n; ++i) cin>>x, posA[x] = i;
	for (i = 0; i < n; ++i) cin>>b[i], posB[b[i]] = i, dists.insert(i - posA[b[i]]);

	x = 0;
	for (i = 0; i < n; ++i)
	{
		auto it = dists.lower_bound(x);
		sol = n;
		if (it != dists.end())
			sol = min(sol, abs(*it - x));
		if (it != dists.begin())
		{
			--it;
			sol = min(sol, abs(*it - x));
		}

		cout<<sol<<endl;

		++x;
		dists.erase(dists.find(posB[b[i]] - posA[b[i]]));
		dists.insert(n + posB[b[i]] - posA[b[i]]);
	}
}


#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
using namespace std;
#define REP(i,n) for((i)=0;(i)<(int)(n);(i)++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
int n;
int a[100010], b[100010];
int x[100010], y[100010];
multiset <int> s;
int query(int x){
    int ans = (1<<29);
    __typeof(s.begin()) itr = s.lower_bound(x);
    if(itr != s.end()){
        int y = (*itr);
        ans = min(ans, y - x);
    }
    if(itr != s.begin()){
        itr--;
        int y = (*itr);
        ans = min(ans,x - y);
    }
    return ans;
}
int main(){
    scanf("%d",&n);
    for(int i = 0; i < n; ++i){
        scanf("%d", &a[i]);
        a[i]--;
    }
    for(int i = 0; i < n; ++i){
        scanf("%d",&b[i]);
        b[i]--;
    }
    for(int i = 0; i < n; ++i) x[a[i]] = i;
    for(int i = 0; i < n; ++i) y[b[i]] = i;
    for(int i = 0; i < n; ++i)
        s.insert(x[i] - y[i]);
    for(int i = 0; i < n; ++i){
        int ans = query(-i);
        printf("%d\n", ans);
        int tmp = x[b[i]] - y[b[i]];
        s.erase(s.find(tmp));
        s.insert(tmp - n);
    }
    return 0;
}


#include "bits/stdc++.h"
using namespace std;

const int N = 1e5 +5;

typedef long long ll;
int n, a[N], b[N];
multiset<int>st;
int main(){

    int n;
    cin >> n;
    for(int i = 0; i < n; i++){
        int x; cin >> x;
        a[x-1] = i;
    }
    for(int i = 0; i < n && cin >> b[i]; i++)st.insert(i - a[b[i]-1]);
    for(int i = 0; i < n; i++){
        b[i]--;
        auto it = st.lower_bound(i);
        int ans = N;
        if(it != st.end())ans = min(ans, *it - i);
        if(it != st.begin())ans = min(ans, i - *(--it));
        cout << ans << '\n';
        int x = i - a[b[i]];
        st.erase(st.find(x));
        st.insert(x + n);
    }

    return 0 ;
}


#include <bits/stdc++.h>

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define int ll
#define forn(i, a, b) for(int i = (a); i <= (b); ++i)
#define forev(i, b, a) for(int i = (b); i >= (a); --i)
#define VAR(v, i) __typeof(i) v=(i)
#define forit(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> Vll;
typedef vector<pair<int, int> > vpii;
typedef vector<pair<ll, ll> > vpll;

const int maxn = (int)2e5 + 100;
const int mod = (int)1e9 + 7;

#define inf mod

int n, a[maxn], x, pos[maxn];
main () {
  cin >> n;
  forn(i, 1, n) cin >> x, pos[x] = i;
  multiset<int> s1, s2;
  forn(i, 1, n){
    cin >> a[i];
    if(i > pos[a[i]]){
      s1.insert(i - pos[a[i]]);
    }else{
      s2.insert(pos[a[i]] - i);
    }
  }
  s1.insert(inf); s2.insert(inf);
  cout << min(*s1.begin(), *s2.begin()) << endl;
  forn(i, 1, n - 1){
    s2.erase(s2.find(pos[a[i]] - i));
    s1.insert(n - pos[a[i]] + i);
    while(*s1.begin() - i == 0){
      s2.insert(-i);
      s1.erase(s1.begin());
    }
    cout << min(*s1.begin() - i, *s2.begin() + i) << endl;
  }

}


#include <bits/stdc++.h>

#define ll long long
#define pb push_back

using namespace std;
set<pair<int,int> > s1,s2,p2;
int delta1=0,delta2=0;
void ispisi()
{
    set<pair<int,int> >::iterator it;
    printf("S1 delta: %i, sadrzi:\n",delta1);
    for(it=s1.begin();it!=s1.end();it++)
    {
        printf("%i %i\n",(*it).first,(*it).second);
    }
    printf("S2 delta: %i, sadrzi:\n",delta2);
    for(it=s2.begin();it!=s2.end();it++)
    {
        printf("%i %i\n",(*it).first,(*it).second);
    }
    printf("P2 sadrzi:\n");
    for(it=p2.begin();it!=p2.end();it++)
    {
        printf("%i %i\n",(*it).first,(*it).second);
    }
}
int main()
{
    int n;
    scanf("%i",&n);
    vector<int> poz(n+1);
    for(int i=0;i<n;i++)
    {
        int a;
        scanf("%i",&a);
        poz[a]=i;
        //printf("Poz od %i: %i\n",a,poz[a]);
    }
    for(int i=0;i<n;i++)
    {
        int a;
        scanf("%i",&a);
        //printf("Poz od %i: %i\n",a,poz[a]);
        if(i<=poz[a])
        {
            s1.insert({poz[a]-i,a});
        }
        else
        {
            s2.insert({i-poz[a],a});
            p2.insert({i,a});
        }
    }
    //ispisi();
    printf("%i\n",min((*s1.begin()).first-delta1,(*s2.begin()).first+delta2));
    for(int i=1;i<n;i++)
    {
        delta1--;
        delta2++;
        while(s1.size()>0&&(*s1.begin()).first+delta1<0)
        {
            int a=(*s1.begin()).second;
            //printf("Usao za %i\n",a);
            //ispisi();
            s2.insert({1-delta2,a});
            p2.insert({poz[a]+1-delta2,a});
            s1.erase(s1.begin());
            //printf("Na kraju:\n");
            //ispisi();
        }
        set<pair<int,int> >::iterator it;
        it=p2.end();
        it--;
        while(p2.size()>0&&(*it).first+delta2>=n)
        {
            int a=(*it).second;
            //printf("Usao2 za %i\n",a);
            //ispisi();
            s2.erase(s2.find({(*it).first-poz[a],a}));
            s1.insert({poz[a]-delta1,a});
            p2.erase(it);
            if(p2.size()>0)
            {
                it=p2.end();
                it--;
            }
            //printf("Na kraju:\n");
            //ispisi();
        }
        printf("%i\n",min((*s1.begin()).first+delta1,(*s2.begin()).first+delta2));
    }
    return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define pb push_back

using namespace std;
set<pair<int,int> > s1,s2,p2;
int delta1=0,delta2=0;
void ispisi()
{
    set<pair<int,int> >::iterator it;
    printf("S1 delta: %i, sadrzi:\n",delta1);
    for(it=s1.begin();it!=s1.end();it++)
    {
        printf("%i %i\n",(*it).first,(*it).second);
    }
    printf("S2 delta: %i, sadrzi:\n",delta2);
    for(it=s2.begin();it!=s2.end();it++)
    {
        printf("%i %i\n",(*it).first,(*it).second);
    }
    printf("P2 sadrzi:\n");
    for(it=p2.begin();it!=p2.end();it++)
    {
        printf("%i %i\n",(*it).first,(*it).second);
    }
}
int main()
{
    int n;
    scanf("%i",&n);
    vector<int> poz(n+1);
    for(int i=0;i<n;i++)
    {
        int a;
        scanf("%i",&a);
        poz[a]=i;
        //printf("Poz od %i: %i\n",a,poz[a]);
    }
    for(int i=0;i<n;i++)
    {
        int a;
        scanf("%i",&a);
        //printf("Poz od %i: %i\n",a,poz[a]);
        if(i>=poz[a])
        {
            s1.insert({i-poz[a],a});
        }
        else
        {
            s2.insert({poz[a]-i,a});
            p2.insert({i,a});
        }
    }
    //ispisi();
    printf("%i\n",min((*s1.begin()).first-delta1,(*s2.begin()).first+delta2));
    for(int i=1;i<n;i++)
    {
        delta1--;
        delta2++;
        while(s1.size()>0&&(*s1.begin()).first+delta1<0)
        {
            int a=(*s1.begin()).second;
            //printf("Usao za %i\n",a);
            //ispisi();
            s2.insert({1-delta2,a});
            p2.insert({poz[a]-1-delta1,a});
            s1.erase(s1.begin());
            //printf("Na kraju:\n");
            //ispisi();
        }
        while(p2.size()>0&&(*p2.begin()).first+delta1<0)
        {
            int a=(*p2.begin()).second;
            //printf("Usao2 za %i\n",a);
            //ispisi();
            s2.erase(s2.find({-(*p2.begin()).first+poz[a],a}));
            s1.insert({n-1-poz[a]-delta1,a});
            p2.erase(p2.begin());
            //printf("Na kraju:\n");
            //ispisi();
        }
        printf("%i\n",min((*s1.begin()).first+delta1,(*s2.begin()).first+delta2));
    }
    return 0;
}


/*
 * DATE : 2017-07-07
 * Algo :
*/
#include<bits/stdc++.h>
using namespace std;
/*------- Constants---- */

#define Long                    long long
#define Ulong                   unsigned long long
#define FOR(I,A,B)              for(long long i=0; i < n ; i++ )
#define REP(i,n)                for(long long i=0; i < n ; i++ )
#define mp                      make_pair
#define pb                      push_back
#define all(x)                  (x).begin(),(x).end()
#define PI                      acos(-1.0)
#define EPS                     1e-9
#define F                       first
#define S                       second
#define di(x)                   int x; input(x)
#define in(x)                   input(x)
#define in2(x,y)                in(x),in(y)
#define in3(x,y,z)              in(x),in2(y,z)
#define lc                      ((n)<<1)
#define rc                      ((n)<<1|1)
#define db(x)                   cout << #x << " -> " << x << endl;
#define ms(ara_name,value)      memset(ara_name,value,sizeof(ara_name))
#define IO                      ios_base::sync_with_stdio(0);cin.tie(0)
#define READ                    freopen("in.txt","r",stdin)
#define WRITE                   freopen("out.txt","w",stdout)
template<class T> inline void input(T &x) {
    register char c = getchar();x = 0;
    int neg = 0;
    for(; ((c<48 || c>57) && c != '-'); c = getchar());
    if(c=='-'){neg = 1;c = getchar();}
    for(; c>47 && c<58 ; c = getchar()){x = (x<<1) + (x<<3) + c - 48;}
    if(neg) x = -x;
}
inline long long bigmod(long long p,long long e,long long M){
    long long ret = 1;
    for(; e > 0; e >>= 1){
        if(e & 1) ret = (ret * p) % M;
        p = (p * p) % M;
    } return ret;
}
template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);}

/***************************** END OF TEMPLATE *******************************/
const int N = 1e5+5;

struct Node {
    int v,x;
    bool operator<(const Node &p) const {
        if(v == p.v) return x < p.x;
        return v < p.v;
    }
};

set<Node> L,R;
map<int,int> Map, Map2;
int a[N],b[N],x[N];
int V[N];
int P[N];
int main()
{
    srand(time(NULL));
    int n;
    in(n);
    for(int i = 1; i <= n; i ++ ) x[i ] = i;
    random_shuffle(x + 1, x + n + 1);
    for(int i = 1;  i <= n; i ++ ) a[i] = x[i] ;// printf("%d ",a[i]); puts("");
    random_shuffle(x + 1, x + n + 1);
    for(int i = 1;  i <= n; i ++ ) b[i] = x[i] ;//printf("%d ",b[i]); puts("") ;

//    for(int i = 1;i <= n; i ++ ) in(a[i]);
//    for(int i = 1;i <= n; i ++ ) in(b[i]);
    reverse(a + 1, a + n + 1);
    reverse(b + 1, b + n + 1);
    for(int i = 1;i <= n; i ++ ) Map[a[i]] = i;
    for(int i = 1;i <= n; i ++ ) Map2[b[i]] = i;

    for(int i = 1; i <= n;i ++ ) {
        int now = b[i];
        if(i <= Map[now]) L.insert({ Map[now] - i, now}), V[now] = Map[now] - i , P[now] = 0;
        else R.insert({ i - Map[now], now}) , V[now]  = i - Map[now], P[now] = 1;
    }
    int lo = 0, ri = 0;
    int ptr = n;

    for(int i = 1; i <= n; i ++ ) {
//        printf("IN L :\n");
//        for(auto x: L) cout << x.x <<" " << x.v +lo << endl;
//        printf("IN R :\n");
//        for(auto x: R) cout << x.x <<" " << x.v +ri << endl;

        int ans = 1e8;
        if(L.size() ) ans = min( ans, lo + L.begin() -> v );
        if(R.size() ) ans = min( ans, ri + R.begin() -> v );

        vector<int> v;
        for(auto x: L ) {
            if( x.v + lo == 0 ) v.push_back(x.x), V[x.x] = x.v;
            else break;
        }
        for(auto x: v ) {
            if(L.find({V[x],x}) != L.end()) L.erase({V[x], x});
            R.insert({ -ri , x});
            V[x] = -ri;
            P[x] = 1;
        }
        int last = b[ptr];
        if(P[last] == 1 ) {
            if(R.find({V[last] , last}) != R.end()) R.erase({ V[last] , last });
            L.insert({ Map[last] - lo , last});
            P[last] = 0;
            V[last] = Map[last] - lo;
        }
        lo--;
        ri++;
        ptr--;
//        int br = 1e8;
//        for(auto x: Map) {
//            br = min(br , abs(x.second - Map2[x.first]));
//        }
        //printf("%d\n",ans);
//        if(ans != br) assert(0);
//        for(auto& x: Map2) {
//            x.second ++;
//            if(x.second == n + 1) x.second = 1;
//        }

    }
    cout << "END" << endl;

    return 0;
}


/*
 * DATE : 2017-07-07
 * Algo :
*/
#include<bits/stdc++.h>
using namespace std;
/*------- Constants---- */

#define Long                    long long
#define Ulong                   unsigned long long
#define FOR(I,A,B)              for(long long i=0; i < n ; i++ )
#define REP(i,n)                for(long long i=0; i < n ; i++ )
#define mp                      make_pair
#define pb                      push_back
#define all(x)                  (x).begin(),(x).end()
#define PI                      acos(-1.0)
#define EPS                     1e-9
#define F                       first
#define S                       second
#define di(x)                   int x; input(x)
#define in(x)                   input(x)
#define in2(x,y)                in(x),in(y)
#define in3(x,y,z)              in(x),in2(y,z)
#define lc                      ((n)<<1)
#define rc                      ((n)<<1|1)
#define db(x)                   cout << #x << " -> " << x << endl;
#define ms(ara_name,value)      memset(ara_name,value,sizeof(ara_name))
#define IO                      ios_base::sync_with_stdio(0);cin.tie(0)
#define READ                    freopen("in.txt","r",stdin)
#define WRITE                   freopen("out.txt","w",stdout)
template<class T> inline void input(T &x) {
    register char c = getchar();x = 0;
    int neg = 0;
    for(; ((c<48 || c>57) && c != '-'); c = getchar());
    if(c=='-'){neg = 1;c = getchar();}
    for(; c>47 && c<58 ; c = getchar()){x = (x<<1) + (x<<3) + c - 48;}
    if(neg) x = -x;
}
inline long long bigmod(long long p,long long e,long long M){
    long long ret = 1;
    for(; e > 0; e >>= 1){
        if(e & 1) ret = (ret * p) % M;
        p = (p * p) % M;
    } return ret;
}
template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);}

/***************************** END OF TEMPLATE *******************************/
const int N = 1e5+5;

struct Node {
    int v,x;
    bool operator<(const Node &p) const {
        if(v == p.v) return x < p.x;
        return v < p.v;
    }
};

set<Node> L,R;
map<int,int> Map, Map2;
int a[N],b[N],x[N];
int V[N];
int P[N];
int main()
{
    srand(time(NULL));
    int n;
    in(n);
//    for(int i = 1; i <= n; i ++ ) x[i ] = i;
//    random_shuffle(x + 1, x + n + 1);
//    for(int i = 1;  i <= n; i ++ ) a[i] = x[i] , printf("%d ",a[i]); puts("");
//    random_shuffle(x + 1, x + n + 1);
//    for(int i = 1;  i <= n; i ++ ) b[i] = x[i], printf("%d ",b[i]); puts("") ;

    for(int i = 1;i <= n; i ++ ) in(a[i]);
    for(int i = 1;i <= n; i ++ ) in(b[i]);
    reverse(a + 1, a + n + 1);
    reverse(b + 1, b + n + 1);
    for(int i = 1;i <= n; i ++ ) Map[a[i]] = i;
    for(int i = 1;i <= n; i ++ ) Map2[b[i]] = i;

    for(int i = 1; i <= n;i ++ ) {
        int now = b[i];
        if(i <= Map[now]) L.insert({ Map[now] - i, now}), V[now] = Map[now] - i , P[now] = 0;
        else R.insert({ i - Map[now], now}) , V[now]  = i - Map[now], P[now] = 1;
    }
    int lo = 0, ri = 0;
    int ptr = n;

    for(int i = 1; i <= n; i ++ ) {
//        printf("IN L :\n");
//        for(auto x: L) cout << x.x <<" " << x.v +lo << endl;
//        printf("IN R :\n");
//        for(auto x: R) cout << x.x <<" " << x.v +ri << endl;

        int ans = 1e8;
        if(L.size() ) ans = min( ans, lo + L.begin() -> v );
        if(R.size() ) ans = min( ans, ri + R.begin() -> v );

        vector<int> v;
        for(auto x: L ) {
            if( x.v + lo == 0 ) v.push_back(x.x), V[x.x] = x.v;
        }
        for(auto x: v ) {
            if(L.find({V[x],x}) != L.end()) L.erase({V[x], x});
            R.insert({ -ri , x});
            V[x] = -ri;
            P[x] = 1;
        }
        int last = b[ptr];
        if(P[last] == 1 ) {
            if(R.find({V[last] , last}) != R.end()) R.erase({ V[last] , last });
            L.insert({ Map[last] - lo , last});
            P[last] = 0;
            V[last] = Map[last] - lo;
        }
        lo--;
        ri++;
        ptr--;
//        int br = 1e8;
//        for(auto x: Map) {
//            br = min(br , abs(x.second - Map2[x.first]));
//        }
        printf("%d\n",ans);
//        if(ans != br) assert(0);
//        for(auto& x: Map2) {
//            x.second ++;
//            if(x.second == n + 1) x.second = 1;
//        }

    }

    return 0;
}


/*
 * DATE : 2017-07-07
 * Algo :
*/
#include<bits/stdc++.h>
using namespace std;
/*------- Constants---- */

#define Long                    long long
#define Ulong                   unsigned long long
#define FOR(I,A,B)              for(long long i=0; i < n ; i++ )
#define REP(i,n)                for(long long i=0; i < n ; i++ )
#define mp                      make_pair
#define pb                      push_back
#define all(x)                  (x).begin(),(x).end()
#define PI                      acos(-1.0)
#define EPS                     1e-9
#define F                       first
#define S                       second
#define di(x)                   int x; input(x)
#define in(x)                   input(x)
#define in2(x,y)                in(x),in(y)
#define in3(x,y,z)              in(x),in2(y,z)
#define lc                      ((n)<<1)
#define rc                      ((n)<<1|1)
#define db(x)                   cout << #x << " -> " << x << endl;
#define ms(ara_name,value)      memset(ara_name,value,sizeof(ara_name))
#define IO                      ios_base::sync_with_stdio(0);cin.tie(0)
#define READ                    freopen("in.txt","r",stdin)
#define WRITE                   freopen("out.txt","w",stdout)
template<class T> inline void input(T &x) {
    register char c = getchar();x = 0;
    int neg = 0;
    for(; ((c<48 || c>57) && c != '-'); c = getchar());
    if(c=='-'){neg = 1;c = getchar();}
    for(; c>47 && c<58 ; c = getchar()){x = (x<<1) + (x<<3) + c - 48;}
    if(neg) x = -x;
}
inline long long bigmod(long long p,long long e,long long M){
    long long ret = 1;
    for(; e > 0; e >>= 1){
        if(e & 1) ret = (ret * p) % M;
        p = (p * p) % M;
    } return ret;
}
template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);}

/***************************** END OF TEMPLATE *******************************/
const int N = 1e5+5;

struct Node {
    int v,x;
    bool operator<(const Node &p) const {
        if(v == p.v) return x < p.x;
        return v < p.v;
    }
};

set<Node> L,R;
map<int,int> Map, Map2;
int a[N],b[N],x[N];
int V[N];
int P[N];
int main()
{
    srand(time(NULL));
    int n;
    in(n);
//    for(int i = 1; i <= n; i ++ ) x[i ] = i;
//    random_shuffle(x + 1, x + n + 1);
//    for(int i = 1;  i <= n; i ++ ) a[i] = x[i] ;// printf("%d ",a[i]); puts("");
//    random_shuffle(x + 1, x + n + 1);
//    for(int i = 1;  i <= n; i ++ ) b[i] = x[i] ;//printf("%d ",b[i]); puts("") ;

    for(int i = 1;i <= n; i ++ ) in(a[i]);
    for(int i = 1;i <= n; i ++ ) in(b[i]);
    reverse(a + 1, a + n + 1);
    reverse(b + 1, b + n + 1);
    for(int i = 1;i <= n; i ++ ) Map[a[i]] = i;
    for(int i = 1;i <= n; i ++ ) Map2[b[i]] = i;

    for(int i = 1; i <= n;i ++ ) {
        int now = b[i];
        if(i <= Map[now]) L.insert({ Map[now] - i, now}), V[now] = Map[now] - i , P[now] = 0;
        else R.insert({ i - Map[now], now}) , V[now]  = i - Map[now], P[now] = 1;
    }
    int lo = 0, ri = 0;
    int ptr = n;

    for(int i = 1; i <= n; i ++ ) {
//        printf("IN L :\n");
//        for(auto x: L) cout << x.x <<" " << x.v +lo << endl;
//        printf("IN R :\n");
//        for(auto x: R) cout << x.x <<" " << x.v +ri << endl;

        int ans = 1e8;
        if(L.size() ) ans = min( ans, lo + L.begin() -> v );
        if(R.size() ) ans = min( ans, ri + R.begin() -> v );

        vector<int> v;
        for(auto x: L ) {
            if( x.v + lo == 0 ) v.push_back(x.x), V[x.x] = x.v;
            else break;
        }
        for(auto x: v ) {
            if(L.find({V[x],x}) != L.end()) L.erase({V[x], x});
            R.insert({ -ri , x});
            V[x] = -ri;
            P[x] = 1;
        }
        int last = b[ptr];
        if(P[last] == 1 ) {
            if(R.find({V[last] , last}) != R.end()) R.erase({ V[last] , last });
            L.insert({ Map[last] - lo , last});
            P[last] = 0;
            V[last] = Map[last] - lo;
        }
        lo--;
        ri++;
        ptr--;
//        int br = 1e8;
//        for(auto x: Map) {
//            br = min(br , abs(x.second - Map2[x.first]));
//        }
        printf("%d\n",ans);
//        if(ans != br) assert(0);
//        for(auto& x: Map2) {
//            x.second ++;
//            if(x.second == n + 1) x.second = 1;
//        }

    }

    return 0;
}


/*
 * DATE : 2017-07-07
 * Algo :
*/
#include<bits/stdc++.h>
using namespace std;
/*------- Constants---- */

#define Long                    long long
#define Ulong                   unsigned long long
#define FOR(I,A,B)              for(long long i=0; i < n ; i++ )
#define REP(i,n)                for(long long i=0; i < n ; i++ )
#define mp                      make_pair
#define pb                      push_back
#define all(x)                  (x).begin(),(x).end()
#define PI                      acos(-1.0)
#define EPS                     1e-9
#define F                       first
#define S                       second
#define di(x)                   int x; input(x)
#define in(x)                   input(x)
#define in2(x,y)                in(x),in(y)
#define in3(x,y,z)              in(x),in2(y,z)
#define lc                      ((n)<<1)
#define rc                      ((n)<<1|1)
#define db(x)                   cout << #x << " -> " << x << endl;
#define ms(ara_name,value)      memset(ara_name,value,sizeof(ara_name))
#define IO                      ios_base::sync_with_stdio(0);cin.tie(0)
#define READ                    freopen("in.txt","r",stdin)
#define WRITE                   freopen("out.txt","w",stdout)
template<class T> inline void input(T &x) {
    register char c = getchar();x = 0;
    int neg = 0;
    for(; ((c<48 || c>57) && c != '-'); c = getchar());
    if(c=='-'){neg = 1;c = getchar();}
    for(; c>47 && c<58 ; c = getchar()){x = (x<<1) + (x<<3) + c - 48;}
    if(neg) x = -x;
}
inline long long bigmod(long long p,long long e,long long M){
    long long ret = 1;
    for(; e > 0; e >>= 1){
        if(e & 1) ret = (ret * p) % M;
        p = (p * p) % M;
    } return ret;
}
template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);}

/***************************** END OF TEMPLATE *******************************/
const int N = 1e5+5;

struct Node {
    int v,x;
    bool operator<(const Node &p) const {
        if(v == p.v) return x < p.x;
        return v < p.v;
    }
};

set<Node> L,R;
map<int,int> Map, Map2;
int a[N],b[N],x[N];
int V[N];
int P[N];
int main()
{
    srand(time(NULL));
    int n;
    in(n);
//    for(int i = 1; i <= n; i ++ ) x[i ] = i;
//    random_shuffle(x + 1, x + n + 1);
//    for(int i = 1;  i <= n; i ++ ) a[i] = x[i] , printf("%d ",a[i]); puts("");
//    random_shuffle(x + 1, x + n + 1);
//    for(int i = 1;  i <= n; i ++ ) b[i] = x[i], printf("%d ",b[i]); puts("") ;

    for(int i = 1;i <= n; i ++ ) in(a[i]);
    for(int i = 1;i <= n; i ++ ) in(b[i]);
    reverse(a + 1, a + n + 1);
    reverse(b + 1, b + n + 1);
    for(int i = 1;i <= n; i ++ ) Map[a[i]] = i;
    for(int i = 1;i <= n; i ++ ) Map2[b[i]] = i;

    for(int i = 1; i <= n;i ++ ) {
        int now = b[i];
        if(i <= Map[now]) L.insert({ Map[now] - i, now}), V[now] = Map[now] - i , P[now] = 0;
        else R.insert({ i - Map[now], now}) , V[now]  = i - Map[now], P[now] = 1;
    }
    int lo = 0, ri = 0;
    int ptr = n;

    for(int i = 1; i <= n; i ++ ) {
//        printf("IN L :\n");
//        for(auto x: L) cout << x.x <<" " << x.v +lo << endl;
//        printf("IN R :\n");
//        for(auto x: R) cout << x.x <<" " << x.v +ri << endl;

        int ans = 1e8;
        if(L.size() ) ans = min( ans, lo + L.begin() -> v );
        if(R.size() ) ans = min( ans, ri + R.begin() -> v );

        vector<int> v;
        for(auto x: L ) {
            if( x.v + lo == 0 ) v.push_back(x.x);
        }
        for(auto x: v ) {
            L.erase({V[x], x});
            R.insert({ -ri , x});
            V[x] = -ri;
            P[x] = 1;
        }
        int last = b[ptr];
        if(P[last] == 1 ) {
            R.erase({ V[last] , last });
            L.insert({ Map[last] - lo , last});
            P[last] = 0;
            V[last] = Map[last] - lo;
        }
        lo--;
        ri++;
        ptr--;
//        int br = 1e8;
//        for(auto x: Map) {
//            br = min(br , abs(x.second - Map2[x.first]));
//        }
        cout << ans <<endl;
//        if(ans != br) assert(0);
//        for(auto& x: Map2) {
//            x.second ++;
//            if(x.second == n + 1) x.second = 1;
//        }

    }

    return 0;
}


/*
 * DATE : 2017-07-07
 * Algo :
*/
#include<bits/stdc++.h>
using namespace std;
/*------- Constants---- */

#define Long                    long long
#define Ulong                   unsigned long long
#define FOR(I,A,B)              for(long long i=0; i < n ; i++ )
#define REP(i,n)                for(long long i=0; i < n ; i++ )
#define mp                      make_pair
#define pb                      push_back
#define all(x)                  (x).begin(),(x).end()
#define PI                      acos(-1.0)
#define EPS                     1e-9
#define F                       first
#define S                       second
#define di(x)                   int x; input(x)
#define in(x)                   input(x)
#define in2(x,y)                in(x),in(y)
#define in3(x,y,z)              in(x),in2(y,z)
#define lc                      ((n)<<1)
#define rc                      ((n)<<1|1)
#define db(x)                   cout << #x << " -> " << x << endl;
#define ms(ara_name,value)      memset(ara_name,value,sizeof(ara_name))
#define IO                      ios_base::sync_with_stdio(0);cin.tie(0)
#define READ                    freopen("in.txt","r",stdin)
#define WRITE                   freopen("out.txt","w",stdout)
template<class T> inline void input(T &x) {
    register char c = getchar();x = 0;
    int neg = 0;
    for(; ((c<48 || c>57) && c != '-'); c = getchar());
    if(c=='-'){neg = 1;c = getchar();}
    for(; c>47 && c<58 ; c = getchar()){x = (x<<1) + (x<<3) + c - 48;}
    if(neg) x = -x;
}
inline long long bigmod(long long p,long long e,long long M){
    long long ret = 1;
    for(; e > 0; e >>= 1){
        if(e & 1) ret = (ret * p) % M;
        p = (p * p) % M;
    } return ret;
}
template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);}

const long long is_query = -(1LL<<62);
struct Line {
    long long m, b;
    mutable function<const Line*()> succ;
    bool operator<(const Line& rhs) const {
        if (rhs.b != is_query) return m < rhs.m;
        const Line* s = succ();
        if (!s) return 0;
        long long x = rhs.m;
        return b - s->b < (s->m - m) * x;
    }
};
struct HullDynamic : public multiset<Line> { // will maintain upper hull for maximum
    bool bad(iterator y) {
        auto z = next(y);
        if (y == begin()) {
            if (z == end()) return 0;
            return y->m == z->m && y->b <= z->b;
        }
        auto x = prev(y);
        if (z == end()) return y->m == x->m && y->b <= x->b;
        return (x->b - y->b)*(z->m - y->m) >= (y->b - z->b)*(y->m - x->m);
    }
    void addLine(long long m, long long b) {
        auto y = insert({ m, b });
        y->succ = [=] { return next(y) == end() ? 0 : &*next(y); };
        if (bad(y)) { erase(y); return; }
        while (next(y) != end() && bad(next(y))) erase(next(y));
        while (y != begin() && bad(prev(y))) erase(prev(y));
    }
    long long eval(long long x) {
        auto l = *lower_bound((Line) { x, is_query });
        return l.m * x + l.b;
    }
};
/***************************** END OF TEMPLATE *******************************/
const int N = 1e5+5;

struct Node {
    int v,x;
    bool operator<(const Node &p) const {
        return v < p.v;
    }
};

set<Node> L,R;
map<int,int> Map;
int a[N],b[N];
int V[N];
int P[N];
int main()
{

    int n;
    in(n);
    for(int i = 1;i <= n; i ++ ) in(a[i]), Map[a[i]] = i;
    for(int i = 1;i <= n; i ++ ) in(b[i]);

    for(int i = 1; i <= n;i ++ ) {
        int now = b[i];
        if(i <= Map[now]) L.insert({ Map[now] - i, now}), V[now] = Map[now] - i , P[now] = 0;
        else R.insert({ i - Map[now], now}) , V[now]  = i - Map[now], P[now] = 1;
    }
    int lo = 0, ri = 0;
    int ptr = n;
    for(int i = 1; i <= n; i ++ ) {
        int ans = 1e8;
        if(L.size() ) ans = min( ans, lo + L.begin() -> v );
        if(R.size() ) ans = min( ans, ri + R.begin() -> v );
        cout << ans << endl;
        vector<int> v;
        for(auto x: L ) {
            if( x.v + lo == 0 ) v.push_back(x.x);
        }
        for(auto x: v ) {
            L.erase({V[x], x});
            R.insert({ -ri , x});
            V[x] = -ri;
        }
        int last = b[ptr];
        if(P[last] == 1 ) {
            R.erase({ V[last] , last });
            L.insert({ Map[last] - lo, last});
            P[last] = 0;
            V[last] = Map[last] - lo;
        }
        lo--;
        ri++;
        ptr--;

    }

    return 0;
}