Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return 0
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
N = int(input())
xseq = []
yseq = []
uf = UnionFind(N)
ans = 0

for i in range(N):
    x,y = (int(x) for x in input().split())
    xseq.append((x,i))
    yseq.append((y,i))
xseq.sort()
yseq.sort()
xdiff = [(xseq[i][0]-xseq[i-1][0],(xseq[i][1],xseq[i-1][1])) for i in range(1,N)]
ydiff = [(yseq[i][0]-yseq[i-1][0],(yseq[i][1],yseq[i-1][1])) for i in range(1,N)]
diff = xdiff + ydiff
diff.sort()
d = deque(diff)

for i in range(len(d)):
    temp = d.popleft()
    if uf.find(temp[1][0]) != uf.find(temp[1][1]):
        uf.union(temp[1][0],temp[1][1])
        ans += temp[0]
print(ans)

Fixed submission:
from collections import deque
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return 0
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
N = int(input())
xseq = []
yseq = []
uf = UnionFind(N)
ans = 0

for i in range(N):
    x,y = (int(x) for x in input().split())
    xseq.append((x,i))
    yseq.append((y,i))
xseq.sort()
yseq.sort()
xdiff = [(xseq[i][0]-xseq[i-1][0],(xseq[i][1],xseq[i-1][1])) for i in range(1,N)]
ydiff = [(yseq[i][0]-yseq[i-1][0],(yseq[i][1],yseq[i-1][1])) for i in range(1,N)]
diff = xdiff + ydiff
diff.sort()
d = deque(diff)

for i in range(len(d)):
    temp = d.popleft()
    if uf.find(temp[1][0]) != uf.find(temp[1][1]):
        uf.union(temp[1][0],temp[1][1])
        ans += temp[0]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return 0
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
N = int(input())
same = set()
xseq = []
yseq = []
uf = UnionFind(N)
ans = 0

for i in range(N):
    x,y = (int(x) for x in input().split())
    xseq.append((x,i))
    yseq.append((y,i))
xseq.sort()
yseq.sort()
xdiff = [(xseq[i][0]-xseq[i-1][0],(xseq[i][1],xseq[i-1][1])) for i in range(1,N)]
ydiff = [(yseq[i][0]-yseq[i-1][0],(yseq[i][1],yseq[i-1][1])) for i in range(1,N)]
diff = xdiff + ydiff
diff.sort()
d = deque(diff)

for i in range(len(d)):
    temp = d.popleft()
    if uf.find(temp[1][0]) != uf.find(temp[1][1]):
        uf.union(temp[1][0],temp[1][1])
        ans += temp[0]
    if uf.parents.count(-1*N) == 1:
        break
print(ans)

Fixed submission:
from collections import deque
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return 0
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
N = int(input())
xseq = []
yseq = []
uf = UnionFind(N)
ans = 0

for i in range(N):
    x,y = (int(x) for x in input().split())
    xseq.append((x,i))
    yseq.append((y,i))
xseq.sort()
yseq.sort()
xdiff = [(xseq[i][0]-xseq[i-1][0],(xseq[i][1],xseq[i-1][1])) for i in range(1,N)]
ydiff = [(yseq[i][0]-yseq[i-1][0],(yseq[i][1],yseq[i-1][1])) for i in range(1,N)]
diff = xdiff + ydiff
diff.sort()
d = deque(diff)

for i in range(len(d)):
    temp = d.popleft()
    if uf.find(temp[1][0]) != uf.find(temp[1][1]):
        uf.union(temp[1][0],temp[1][1])
        ans += temp[0]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return 0
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
N = int(input())
same = set()
xseq = []
yseq = []
uf = UnionFind(N)
ans = 0

for i in range(N):
    x,y = (int(x) for x in input().split())
    xseq.append((x,i))
    yseq.append((y,i))
xseq.sort()
yseq.sort()
xdiff = [(xseq[i][0]-xseq[i-1][0],(xseq[i][1],xseq[i-1][1])) for i in range(1,N)]
ydiff = [(yseq[i][0]-yseq[i-1][0],(yseq[i][1],yseq[i-1][1])) for i in range(1,N)]
diff = xdiff + ydiff
diff.sort()
d = deque(diff)

for i in range(len(d)):
    temp = d.popleft()
    if uf.find(temp[1][0]) != uf.find(temp[1][1]):
        uf.union(temp[1][0],temp[1][1])
        ans += temp[0]
    if uf.parents.count(-1*N) == 1:
        break
print(ans)

Fixed submission:
from collections import deque
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return 0
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
N = int(input())
xseq = []
yseq = []
uf = UnionFind(N)
ans = 0

for i in range(N):
    x,y = (int(x) for x in input().split())
    xseq.append((x,i))
    yseq.append((y,i))
xseq.sort()
yseq.sort()
xdiff = [(xseq[i][0]-xseq[i-1][0],(xseq[i][1],xseq[i-1][1])) for i in range(1,N)]
ydiff = [(yseq[i][0]-yseq[i-1][0],(yseq[i][1],yseq[i-1][1])) for i in range(1,N)]
diff = xdiff + ydiff
diff.sort()
d = deque(diff)

for i in range(len(d)):
    temp = d.popleft()
    if uf.find(temp[1][0]) != uf.find(temp[1][1]):
        uf.union(temp[1][0],temp[1][1])
        ans += temp[0]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
from collections import deque
class UnionFind(): #Union-Find木の実装
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return 0
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
N = int(input())
same = set()
xseq = []
yseq = []
edge = []
uf = UnionFind(N)
ans = 0

x,y = (int(x) for x in input().split())
same.add((x,y))
xseq.append((x,0))
yseq.append((y,0))
for i in range(1,N):
    x,y = (int(x) for x in input().split())
    if (x,y) not in same:
        same.add((x,y))
        xb = bisect.bisect_left(xseq,(x,i))
        xseq.insert(xb,(x,i))
        if xb == 0:
            edge.append((xseq[1][0]-x,(xseq[1][1],i)))
        elif xb == len(xseq)-1:
            edge.append((x-xseq[-2][0],(xseq[-2][1],i)))
        else:
            edge.append((xseq[xb+1][0]-x,(xseq[xb+1][1],i)))
            edge.append((x-xseq[xb-1][0],(xseq[xb-1][1],i)))
        yb = bisect.bisect_left(yseq,(y,i))
        yseq.insert(yb,(y,i))
        if yb == 0:
            edge.append((yseq[1][0]-y,(yseq[1][1],i)))
        elif yb == len(yseq)-1:
            edge.append((y-yseq[-2][0],(yseq[-2][1],i)))
        else:
            edge.append((yseq[yb+1][0]-y,(yseq[yb+1][1],i)))
            edge.append((y-yseq[yb-1][0],(yseq[yb-1][1],i)))
edge.sort()
d = deque(edge)
for i in range(len(d)):
    temp = d.popleft()
    if uf.find(temp[1][0]) != uf.find(temp[1][1]):
        uf.union(temp[1][0],temp[1][1])
        ans += temp[0]
    if uf.parents.count(-1*N) == 1:
        break
print(ans)

Fixed submission:
from collections import deque
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return 0
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
N = int(input())
xseq = []
yseq = []
uf = UnionFind(N)
ans = 0

for i in range(N):
    x,y = (int(x) for x in input().split())
    xseq.append((x,i))
    yseq.append((y,i))
xseq.sort()
yseq.sort()
xdiff = [(xseq[i][0]-xseq[i-1][0],(xseq[i][1],xseq[i-1][1])) for i in range(1,N)]
ydiff = [(yseq[i][0]-yseq[i-1][0],(yseq[i][1],yseq[i-1][1])) for i in range(1,N)]
diff = xdiff + ydiff
diff.sort()
d = deque(diff)

for i in range(len(d)):
    temp = d.popleft()
    if uf.find(temp[1][0]) != uf.find(temp[1][1]):
        uf.union(temp[1][0],temp[1][1])
        ans += temp[0]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
from collections import deque
class UnionFind(): #Union-Find木の実装
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return 0
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
N = int(input())
same = set()
xseq = []
yseq = []
edge = []
uf = UnionFind(N)
ans = 0

x,y = (int(x) for x in input().split())
same.add((x,y))
xseq.append((x,0))
yseq.append((y,0))
for i in range(1,N):
    x,y = (int(x) for x in input().split())
    if (x,y) not in same:
        same.add((x,y))
        xb = bisect.bisect_left(xseq,(x,i))
        xseq.insert(xb,(x,i))
        if xb == 0:
            edge.append((xseq[1][0]-x,(xseq[1][1],i)))
        elif xb == len(xseq)-1:
            edge.append((x-xseq[-2][0],(xseq[-2][1],i)))
        else:
            edge.append((xseq[xb+1][0]-x,(xseq[xb+1][1],i)))
            edge.append((x-xseq[xb-1][0],(xseq[xb-1][1],i)))
        yb = bisect.bisect_left(yseq,(y,i))
        yseq.insert(yb,(y,i))
        if yb == 0:
            edge.append((yseq[1][0]-y,(yseq[1][1],i)))
        elif yb == len(yseq)-1:
            edge.append((y-yseq[-2][0],(yseq[-2][1],i)))
        else:
            edge.append((yseq[yb+1][0]-y,(yseq[yb+1][1],i)))
            edge.append((y-yseq[yb-1][0],(yseq[yb-1][1],i)))
edge.sort()
d = deque(edge)
for i in range(len(d)):
    temp = d.popleft()
    if uf.find(temp[1][0]) != uf.find(temp[1][1]):
        uf.union(temp[1][0],temp[1][1])
        ans += temp[0]
print(ans)

Fixed submission:
from collections import deque
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return 0
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
N = int(input())
xseq = []
yseq = []
uf = UnionFind(N)
ans = 0

for i in range(N):
    x,y = (int(x) for x in input().split())
    xseq.append((x,i))
    yseq.append((y,i))
xseq.sort()
yseq.sort()
xdiff = [(xseq[i][0]-xseq[i-1][0],(xseq[i][1],xseq[i-1][1])) for i in range(1,N)]
ydiff = [(yseq[i][0]-yseq[i-1][0],(yseq[i][1],yseq[i-1][1])) for i in range(1,N)]
diff = xdiff + ydiff
diff.sort()
d = deque(diff)

for i in range(len(d)):
    temp = d.popleft()
    if uf.find(temp[1][0]) != uf.find(temp[1][1]):
        uf.union(temp[1][0],temp[1][1])
        ans += temp[0]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
from collections import deque
class UnionFind(): #Union-Find木の実装
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return 0
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
N = int(input())
same = set()
xseq = []
yseq = []
edge = []
uf = UnionFind(N)
ans = 0

x,y = (int(x) for x in input().split())
same.add((x,y))
xseq.append((x,0))
yseq.append((y,0))
for i in range(1,N):
    x,y = (int(x) for x in input().split())
    if (x,y) not in same:
        same.add((x,y))
        xb = bisect.bisect_left(xseq,(x,i))
        xseq.insert(xb,(x,i))
        if xb == 0:
            edge.append((xseq[1][0]-x,(xseq[1][1],i)))
        elif xb == len(xseq)-1:
            edge.append((x-xseq[-2][0],(xseq[-2][1],i)))
        else:
            edge.append((xseq[xb+1][0]-x,(xseq[xb+1][1],i)))
            edge.append((x-xseq[xb-1][0],(xseq[xb-1][1],i)))
        yb = bisect.bisect_left(yseq,(y,i))
        yseq.insert(yb,(y,i))
        if yb == 0:
            edge.append((yseq[1][0]-y,(yseq[1][1],i)))
        elif yb == len(yseq)-1:
            edge.append((y-yseq[-2][0],(yseq[-2][1],i)))
        else:
            edge.append((yseq[yb+1][0]-y,(yseq[yb+1][1],i)))
            edge.append((y-yseq[yb-1][0],(yseq[yb-1][1],i)))
edge.sort()
d = deque(edge)
for i in range(len(d)):
    temp = d.popleft()
    if uf.find(temp[1][0]) != uf.find(temp[1][1]):
        uf.union(temp[1][0],temp[1][1])
        ans += temp[0]
print(ans)

Fixed submission:
from collections import deque
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return 0
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
N = int(input())
xseq = []
yseq = []
uf = UnionFind(N)
ans = 0

for i in range(N):
    x,y = (int(x) for x in input().split())
    xseq.append((x,i))
    yseq.append((y,i))
xseq.sort()
yseq.sort()
xdiff = [(xseq[i][0]-xseq[i-1][0],(xseq[i][1],xseq[i-1][1])) for i in range(1,N)]
ydiff = [(yseq[i][0]-yseq[i-1][0],(yseq[i][1],yseq[i-1][1])) for i in range(1,N)]
diff = xdiff + ydiff
diff.sort()
d = deque(diff)

for i in range(len(d)):
    temp = d.popleft()
    if uf.find(temp[1][0]) != uf.find(temp[1][1]):
        uf.union(temp[1][0],temp[1][1])
        ans += temp[0]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
from collections import deque
class UnionFind(): #Union-Find木の実装
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return 0
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
N = int(input())
same = set()
xseq = []
yseq = []
edge = []
uf = UnionFind(N)
ans = 0

x,y = (int(x) for x in input().split())
same.add((x,y))
xseq.append((x,0))
yseq.append((y,0))
for i in range(1,N):
    x,y = (int(x) for x in input().split())
    if (x,y) not in same:
        same.add((x,y))
        xb = bisect.bisect_left(xseq,(x,i))
        xseq.insert(xb,(x,i))
        if xb == 0:
            edge.append((xseq[1][0]-x,(xseq[1][1],i)))
        elif xb == len(xseq)-1:
            edge.append((x-xseq[-2][0],(xseq[-2][1],i)))
        else:
            edge.append((xseq[xb+1][0]-x,(xseq[xb+1][1],i)))
            edge.append((x-xseq[xb-1][0],(xseq[xb-1][1],i)))
        yb = bisect.bisect_left(yseq,(y,i))
        yseq.insert(yb,(y,i))
        if yb == 0:
            edge.append((yseq[1][0]-y,(yseq[1][1],i)))
        elif yb == len(yseq)-1:
            edge.append((y-yseq[-2][0],(yseq[-2][1],i)))
        else:
            edge.append((yseq[yb+1][0]-y,(yseq[yb+1][1],i)))
            edge.append((y-yseq[yb-1][0],(yseq[yb-1][1],i)))
edge.sort()
d = deque([e[0] for e in edge])
for i in range(len(d)):
    temp = d.popleft()
    a,b = edge[i][1][0],edge[i][1][1]
    if uf.find(a) != uf.find(b):
        uf.union(a,b)
        ans += temp
print(ans)

Fixed submission:
from collections import deque
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return 0
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
N = int(input())
xseq = []
yseq = []
uf = UnionFind(N)
ans = 0

for i in range(N):
    x,y = (int(x) for x in input().split())
    xseq.append((x,i))
    yseq.append((y,i))
xseq.sort()
yseq.sort()
xdiff = [(xseq[i][0]-xseq[i-1][0],(xseq[i][1],xseq[i-1][1])) for i in range(1,N)]
ydiff = [(yseq[i][0]-yseq[i-1][0],(yseq[i][1],yseq[i-1][1])) for i in range(1,N)]
diff = xdiff + ydiff
diff.sort()
d = deque(diff)

for i in range(len(d)):
    temp = d.popleft()
    if uf.find(temp[1][0]) != uf.find(temp[1][1]):
        uf.union(temp[1][0],temp[1][1])
        ans += temp[0]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
from collections import deque
class UnionFind(): #Union-Find木の実装
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return 0
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
N = int(input())
same = set()
xseq = []
yseq = []
edge = []
uf = UnionFind(N)
ans = 0

x,y = (int(x) for x in input().split())
same.add((x,y))
xseq.append((x,0))
yseq.append((y,0))
for i in range(1,N):
    x,y = (int(x) for x in input().split())
    if (x,y) not in same:
        same.add((x,y))
        xb = bisect.bisect_left(xseq,(x,i))
        xseq.insert(xb,(x,i))
        if xb == 0:
            edge.append((xseq[1][0]-x,(xseq[1][1],i)))
        elif xb == len(xseq)-1:
            edge.append((x-xseq[-2][0],(xseq[-2][1],i)))
        else:
            edge.append((xseq[xb+1][0]-x,(xseq[xb+1][1],i)))
            edge.append((x-xseq[xb-1][0],(xseq[xb-1][1],i)))
        yb = bisect.bisect_left(yseq,(y,i))
        yseq.insert(yb,(y,i))
        if yb == 0:
            edge.append((yseq[1][0]-y,(yseq[1][1],i)))
        elif yb == len(yseq)-1:
            edge.append((y-yseq[-2][0],(yseq[-2][1],i)))
        else:
            edge.append((yseq[yb+1][0]-y,(yseq[yb+1][1],i)))
            edge.append((y-yseq[yb-1][0],(yseq[yb-1][1],i)))
edge.sort()
d = deque([e[0] for e in edge])
for i in range(len(d)):
    temp = d.popleft()
    a,b = edge[i][1][0],edge[i][1][1]
    if uf.find(a) != uf.find(b):
        uf.union(a,b)
        ans += temp
print(ans)

Fixed submission:
from collections import deque
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return 0
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
N = int(input())
xseq = []
yseq = []
uf = UnionFind(N)
ans = 0

for i in range(N):
    x,y = (int(x) for x in input().split())
    xseq.append((x,i))
    yseq.append((y,i))
xseq.sort()
yseq.sort()
xdiff = [(xseq[i][0]-xseq[i-1][0],(xseq[i][1],xseq[i-1][1])) for i in range(1,N)]
ydiff = [(yseq[i][0]-yseq[i-1][0],(yseq[i][1],yseq[i-1][1])) for i in range(1,N)]
diff = xdiff + ydiff
diff.sort()
d = deque(diff)

for i in range(len(d)):
    temp = d.popleft()
    if uf.find(temp[1][0]) != uf.find(temp[1][1]):
        uf.union(temp[1][0],temp[1][1])
        ans += temp[0]
print(ans)