```go
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"strconv"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanWords)
scanner.Buffer(make([]byte, 1024), 1024*1024)
scanner.Scan()
n, _ := strconv.Atoi(scanner.Text())
posA := make([]int, n+1)
for i := 0; i < n; i++ {
scanner.Scan()
x, _ := strconv.Atoi(scanner.Text())
posA[x] = i
}
b := make([]int, n)
for i := 0; i < n; i++ {
scanner.Scan()
b[i], _ = strconv.Atoi(scanner.Text())
}
val := make([]int, n)
for j := 0; j < n; j++ {
val[j] = posA[b[j]] - j
}
const INF = int(1e9)
rightAns := make([]int, n)
var root *Node
for i := n - 1; i >= 0; i-- {
root = insert(root, val[i])
target := -i
best := INF
if v, ok := floor(root, target); ok {
if diff := abs(v - target); diff < best {
best = diff
}
}
if v, ok := ceil(root, target); ok {
if diff := abs(v - target); diff < best {
best = diff
}
}
rightAns[i] = best
}
leftAns := make([]int, n)
root = nil
for i := 0; i < n; i++ {
target := n - i
best := INF
if v, ok := floor(root, target); ok {
if diff := abs(v - target); diff < best {
best = diff
}
}
if v, ok := ceil(root, target); ok {
if diff := abs(v - target); diff < best {
best = diff
}
}
leftAns[i] = best
root = insert(root, val[i])
}
out := bufio.NewWriter(os.Stdout)
for i := 0; i < n; i++ {
ans := rightAns[i]
if leftAns[i] < ans {
ans = leftAns[i]
}
fmt.Fprintln(out, ans)
}
out.Flush()
}
type Node struct {
val int
priority int
left *Node
right *Node
}
func insert(root *Node, val int) *Node {
if root == nil {
return &Node{val: val, priority: rand.Int()}
}
if val < root.val {
root.left = insert(root.left, val)
if root.left.priority < root.priority {
root = rotateRight(root)
}
} else if val > root.val {
root.right = insert(root.right, val)
if root.right.priority < root.priority {
root = rotateLeft(root)
}
}
return root
}
func rotateRight(p *Node) *Node {
q := p.left
p.left = q.right
q.right = p
return q
}
func rotateLeft(p *Node) *Node {
q := p.right
p.right = q.left
q.left = p
return q
}
func floor(root *Node, key int) (int, bool) {
if root == nil {
return 0, false
}
if root.val == key {
return key, true
}
if root.val < key {
if v, ok := floor(root.right, key); ok {
return v, true
}
return root.val, true
}
return floor(root.left, key)
}
func ceil(root *Node, key int) (int, bool) {
if root == nil {
return 0, false
}
if root.val == key {
return key, true
}
if root.val > key {
if v, ok := ceil(root.left, key); ok {
return v, true
}
return root.val, true
}
return ceil(root.right, key)
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
```