import java.io.*;
import java.util.*;
public class Main {
private static class Read {
StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
public int read() throws IOException {
st.nextToken();
return (int) st.nval;
}
}
public static void main(String[] args) throws IOException {
Read rd = new Read();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
TreeSet<Integer> ts = new TreeSet<>();
int n = rd.read();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
int a = rd.read();
if (i == 0) {
arr[0] = (a < 0) ? -a : a;
} else {
Integer floor = ts.floor(a);
Integer ceiling = ts.ceiling(a);
if (Objects.equals(floor, ceiling)) {
arr[i] = 0;
} else if (floor != null && ceiling != null) {
int num1 = a - floor;
int num2 = ceiling - a;
arr[i] = (num1 < num2) ? num1 : num2;
} else if (floor != null) {
arr[i] = a - floor;
} else {
arr[i] = ceiling - a;
}
}
ts.add(a);
}
ts = null;
int result = 0;
for (int elem : arr) {
result += elem;
}
pw.print(result);
pw.close();
}
}