(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam)))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(defmacro split-ints-and-bind (vars string &body body)
  (let ((pos1 (gensym "POS"))
	(pos2 (gensym "POS"))
	(str (gensym "STR")))
    (labels ((expand (vars &optional (init-pos1 t))
	       (if (null vars)
		   body
		   `((let* ((,pos1 ,(if init-pos1 0 `(1+ ,pos2)))
			    (,pos2 (position #\space ,str :start ,pos1 :test #'char=))
			    (,(car vars) (parse-integer ,str :start ,pos1 :end ,pos2)))
		       ,@(expand (cdr vars) nil))))))
      `(let ((,str ,string))
         (declare (string ,str))
	 ,@(expand vars)))))


(defmacro buffered-read-line (&optional (buffer-size 30) (in '*standard-input*) (terminate-char #\Space))
  "Note that the returned string will be reused."
  (let ((buffer (gensym))
        (character (gensym))
        (idx (gensym)))
    `(let ((,buffer (load-time-value (make-string ,buffer-size
                                                  :element-type 'base-char))))
       (declare (simple-base-string ,buffer))
       (loop for ,character of-type base-char =
                #-swank (code-char (read-byte ,in nil #\Newline))
                #+swank (read-char ,in nil #\Newline)
             for ,idx from 0
             until (char= ,character #\Newline)
             do (setf (schar ,buffer ,idx) ,character)
             finally (setf (schar ,buffer ,idx) ,terminate-char)
                     (return (values ,buffer ,idx))))))

(deftype non-negative-fixnum () '(integer 0 #.most-positive-fixnum))

(defstruct (union-find
            (:constructor make-union-find
                (size &aux (parents (let ((seq (make-array size :element-type 'non-negative-fixnum)))
                                      (dotimes (i size seq) (setf (aref seq i) i))))
                           (ranks (make-array size :element-type 'non-negative-fixnum
                                                   :initial-element 0)))))
  (parents nil :type (simple-array non-negative-fixnum (*)))
  (ranks nil :type (simple-array non-negative-fixnum (*))))

(declaim (ftype (function * (values non-negative-fixnum &optional)) uf-root))
(defun uf-root (x uf-tree)
  "Returns the root of X."
  (declare (optimize (speed 3))
           (non-negative-fixnum x))
  (let ((parents (union-find-parents uf-tree)))
    (if (= x (aref parents x))
        x
        (setf (aref parents x)
              (uf-root (aref parents x) uf-tree)))))

(declaim (inline uf-unite!))
(defun uf-unite! (x1 x2 uf-tree)
  "Unites X1 and X2 destructively."
  (let ((root1 (uf-root x1 uf-tree))
        (root2 (uf-root x2 uf-tree))
        (parents (union-find-parents uf-tree))
        (ranks (union-find-ranks uf-tree)))
    (cond ((= root1 root2) nil)
          ((< (aref ranks root1) (aref ranks root2))
           (setf (aref parents root1) root2))
          ((= (aref ranks root1) (aref ranks root2))
           (setf (aref parents root2) root1)
           (incf (aref ranks root1)))
          (t (setf (aref parents root2) root1)))))

(declaim (inline uf-connected-p))
(defun uf-connected-p (x1 x2 uf-tree)
  "Checks if X1 and X2 have the same root."
  (= (uf-root x1 uf-tree) (uf-root x2 uf-tree)))

;; (defun bench ()
;;   (let* ((size 5000000)
;;          (tree (make-union-find size))
;;          (seed (seed-random-state 0)))
;;     (dotimes (i 5000000)
;;       (uf-unite! (random size seed) (random size seed) tree))))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(defmacro println (obj &optional (stream '*standard-output*))
  `(let ((*read-default-float-format* 'double-float))
     (prog1 (princ ,obj ,stream) (terpri ,stream))))

;; Hauptteil

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         ;; (x y idx)
         (table (make-array n :element-type '(cons uint32 (cons uint32 uint32)))))
    (dotimes (i n)
      (split-ints-and-bind (x y) (buffered-read-line)
        (setf (aref table i) (list x y i))))
    (let ((table-x (sort (copy-seq table) #'< :key #'first))
          (table-y (sort table #'< :key #'second))
          edges) ; (cost from to)
      (declare (list edges))
      (loop for j below (- n 1)
            do (destructuring-bind (x1 y1 city1) (aref table-x j)
                 (destructuring-bind (x2 y2 city2) (aref table-x (+ j 1))
                   (declare (uint32 x1 y1 x2 y2 city1 city2))
                   (push (list (min (abs (- x2 x1)) (abs (- y2 y1)))
                               city1 city2)
                         edges)))
               (destructuring-bind (x1 y1 city1) (aref table-y j)
                 (destructuring-bind (x2 y2 city2) (aref table-y (+ j 1))
                   (declare (uint32 x1 y1 x2 y2 city1 city2))
                   (push (list (min (abs (- x2 x1)) (abs (- y2 y1)))
                               city1 city2)
                         edges)))
            finally (setf edges (sort edges #'< :key #'first)))
      (let ((tree (make-union-find n))
            (cost-sum 0))
        (dolist (edge edges)
          (destructuring-bind (cost from to) edge
            (unless (uf-connected-p from to tree)
              (uf-unite! from to tree)
              (incf cost-sum cost))))
        (println cost-sum)))))

#-swank(main)


Measured memory_bytes: 53856