with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Containers.Vectors;
with Ada.Containers; use Ada.Containers;

with Ada.Text_IO; use Ada.Text_IO;

--  This code reads its input from standard input
--  Use cat _input.txt | historian_hysteria_ to get the result
procedure Historian_Hysteria is

   subtype Location is Positive;
   subtype Distance is Natural;

   package Locations_Vectors is
     new Ada.Containers.Vectors (Index_type => Positive,
                                 Element_Type => Location);
   package Locations_Sorting is new Locations_Vectors.Generic_Sorting;

   Locations_1 : Locations_Vectors.Vector;
   Locations_2 : Locations_Vectors.Vector;

   Total_Distance : Distance := 0;

   type Input_Width_Index is new Positive range 1 .. 2;

   type Matrix is array (Input_Width_Index) of Locations_Vectors.Vector;

   Loca_Matrix : Matrix := (Locations_1, Locations_2);

   Position : Natural := 0;
begin
   while not End_Of_File loop
      declare
         Value : Location;
      begin
         Position := 0;
         while not End_Of_Line loop
            Position := Position + 1;
            Get (Value);
            Loca_Matrix (Input_Width_Index (Position)).Append (Value);
         end loop;
         --  Skip to next Line
         Skip_Line;
      end;
   end loop;

   --  Order the Locations
   for Index in Input_Width_Index'Range loop
      Locations_Sorting.Sort (Loca_Matrix (Index));
   end loop;

   Put_Line ("There are " & Loca_Matrix (1).Length'Image & " values");
   for Index in 1 .. Natural (Loca_Matrix (1).Length) loop
      Total_Distance := Total_Distance
        + abs (Loca_Matrix (1).Element (Index)
               - Loca_Matrix (2).Element (Index));
   end loop;

   Put_Line ("Total distance is " & Total_Distance'Image);
end Historian_Hysteria;