Buggy python submission (verdict: Runtime Error):
import heapq
def cost(a, b, c, d):
    return min(abs(a-c), abs(b-d))

N = int(input())
T, Visit = [], {}
for i in range(N):
    x, y = map(int, input().split())
    Visit[(x, y)] = False
    if (x, y) not in T:
        T.append((x, y))
lenT = len(T)
Tx, Ty = sorted(T, key = lambda t:t[0]), sorted(T, key = lambda t:t[1])

now = Tx[0]
Visit[now] = True
Q = [(cost(Tx[0][0], Tx[0][1], Tx[1][0], Tx[1][1]), Tx[1])]
nowy = Ty.index(now)
if nowy == 0:
    Q.append((cost(now[0], now[1], Ty[1][0], Ty[1][1]), Ty[1]))
elif nowy == lenT-1:
    Q.append((cost(now[0], now[1], Ty[-2][0], Ty[-2][1]), Ty[-2]))
else:
    Q.append((cost(now[0], now[1], Ty[nowy-1][0], Ty[nowy-1][1]), Ty[nowy-1]))
    Q.append((cost(now[0], now[1], Ty[nowy+1][0], Ty[nowy+1][1]), Ty[nowy-1]))
heapq.heapify(Q)
search = [-1, 1]
Total = 0
while Q:
    nowCost, now = heapq.heappop(Q)
    if Visit[now] == False:
        Total += nowCost
        Visit[now] = True
        nowx, nowy = Tx.index(now), Ty.index(now)
        for i in search:
            if 0 <= nowx + i <= lenT-1:
                if Visit[Tx[nowx+i]] == False:
                   heapq.heappush(Q, (cost(now[0], now[1], Tx[nowx+i][0], Tx[nowx+i][1]), Tx[nowx+i]))
            if 0 <= nowy + i <= lenT-1:
                if Visit[Ty[nowy+i]] == False:
                   heapq.heappush(Q, (cost(now[0], now[1], Ty[nowy+i][0], Ty[nowy+i][1]), Ty[nowy+i]))

print(Total)

Fixed submission:
import sys
from operator import itemgetter
import heapq

def solve():
    input = sys.stdin.readline
    N = int(input())
    town = dict()
    road = [None] * N
    roadx, roady = [None] * N, [None] * N
    for i in range(N):
        x, y = map(int, input().split())
        town[(x, y, i)] = i
        road[i] = (x, y, i)
        roadx[i] = (x, y, i)
        roady[i] = (x, y, i)
    roadx.sort()
    roadx_id = dict()
    roady.sort(key = itemgetter(1))
    roady_id = dict()
    for i in range(N):
        roadx_id[roadx[i]] = i
        roady_id[roady[i]] = i

    visited = [False] * N
    q = []
    heapq.heapify(q)
    heapq.heappush(q, (0, 0)) #(cost, town_id)
    cost = 0
    while q:
        add_cost, id = heapq.heappop(q)
        #print(add_cost, id, cost)
        #print(visited)
        if not visited[id]:
            cost += add_cost
            visited[id] = True
            for s in range(2):
                nx = roadx_id[road[id]] + (-1) ** s #roadxにおけるid
                if 0 <= nx < N:
                    if not visited[town[roadx[nx]]]: heapq.heappush(q, (min(abs(road[id][0] - roadx[nx][0]), abs(road[id][1] - roadx[nx][1])), town[roadx[nx]]))
                
                ny = roady_id[road[id]] + (-1) ** s #roadyにおけるid
                if 0 <= ny < N:
                    if not visited[town[roady[ny]]]: heapq.heappush(q, (min(abs(road[id][1] - roady[ny][1]), abs(road[id][1] - roady[ny][1])), town[roady[ny]]))
    
    print(cost)

    return 0

if __name__ == "__main__":
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
def cost(a, b, c, d):
    return min(abs(a-c), abs(b-d))

N = int(input())
T = []
for i in range(N):
    x, y = map(int, input().split())
    if (x, y) not in T:
        T.append((x, y))
lenT = len(T)
Tx, Ty = sorted(T, key = lambda t:t[0]), sorted(T, key = lambda t:t[1])
P = [[] for i in range(lenT)]
for i in range(lenT-1):
    xc, yc = cost(Tx[i][0], Tx[i][1], Tx[i+1][0], Tx[i+1][1]), cost(Ty[i][0], Ty[i][1], Ty[i+1][0], Ty[i+1][1])
    P[T.index(Tx[i])].append([xc, T.index(Tx[i+1])])
    P[T.index(Tx[i+1])].append([xc, T.index(Tx[i])])
    P[T.index(Ty[i])].append([yc, T.index(Ty[i+1])])
    P[T.index(Ty[i+1])].append([yc, T.index(Ty[i])])

Visit = [False for i in range(lenT)]
Visit[0] = True
Q = [p for p in P[0]]
heapq.heapify(Q)
Total = 0
while Q:
    nextCost, nextNode = heapq.heappop(Q)
    if Visit[nextNode] == False:
        Total += nextCost
        Visit[nextNode] = True
        for t in P[nextNode]:
            if Visit[t[1]] == False:
                heapq.heappush(Q, [t[0], t[1]])
print(Total)

Fixed submission:
import sys
from operator import itemgetter
import heapq

def solve():
    input = sys.stdin.readline
    N = int(input())
    town = dict()
    road = [None] * N
    roadx, roady = [None] * N, [None] * N
    for i in range(N):
        x, y = map(int, input().split())
        town[(x, y, i)] = i
        road[i] = (x, y, i)
        roadx[i] = (x, y, i)
        roady[i] = (x, y, i)
    roadx.sort()
    roadx_id = dict()
    roady.sort(key = itemgetter(1))
    roady_id = dict()
    for i in range(N):
        roadx_id[roadx[i]] = i
        roady_id[roady[i]] = i

    visited = [False] * N
    q = []
    heapq.heapify(q)
    heapq.heappush(q, (0, 0)) #(cost, town_id)
    cost = 0
    while q:
        add_cost, id = heapq.heappop(q)
        #print(add_cost, id, cost)
        #print(visited)
        if not visited[id]:
            cost += add_cost
            visited[id] = True
            for s in range(2):
                nx = roadx_id[road[id]] + (-1) ** s #roadxにおけるid
                if 0 <= nx < N:
                    if not visited[town[roadx[nx]]]: heapq.heappush(q, (min(abs(road[id][0] - roadx[nx][0]), abs(road[id][1] - roadx[nx][1])), town[roadx[nx]]))
                
                ny = roady_id[road[id]] + (-1) ** s #roadyにおけるid
                if 0 <= ny < N:
                    if not visited[town[roady[ny]]]: heapq.heappush(q, (min(abs(road[id][1] - roady[ny][1]), abs(road[id][1] - roady[ny][1])), town[roady[ny]]))
    
    print(cost)

    return 0

if __name__ == "__main__":
    solve()