"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");

function print(x) {
  console.log(x);
}
let inputString = "";
let currentLine = 0;

process.stdin.on("data", (inputStdin) => {
  inputString += inputStdin;
});
process.stdin.on("end", () => {
  inputString = inputString.split("\n");
  main();
});
function readline() {
  return inputString[currentLine++];
}

// ********** Code Start **********

function main() {
    var tests = parseInt(readline());

    for (let t = 0; t < tests; t++) {
        var n = parseInt(readline());
        var array = readline().split(' ');
        var arr = [];
        var ans = 0;
        for (let i = 0;i < n;++i) {
            arr.push(parseInt(array[i]));
        }
        // console.log(arr);
        for (let i = 1;i < n;++i) {
            while (arr[n-1-i] >= arr[n-i] && arr[n-1-i] > 0) {
                arr[n-1-i] = parseInt(arr[n-1-i]/2);
                ans++;
            }
            if (arr[n-1-i] == arr[n-i]) {
                ans = -1;
                break;
            }
        }
        // console.log(arr);
        console.log(ans);
    }
}

const rdline = require('readline');
const rl = rdline.createInterface({
    input: process.stdin
});

let lines = [];
rl.on('line', (line) => {
    lines.push(line);
}).on('close', () => {
    // input in lines
    solve();
});

let line = 0;
const readline = () => lines[line++];

solve = () => {
    const t = parseInt(readline());
    for (let j = 0; j < t; j++) {
        var n = parseInt(readline());
        var k = 0;
        var a = readline().split(" ").map(Number);
        for (let i = n - 2; i >= 0; i--) {
            while (a[i] >= a[i + 1] && a[i] > 0) {
                a[i] = Math.floor(a[i] / 2);
                k++;
            }
            if (a[i] === a[i + 1]) {
                k = -1;
            }
        }
        console.log(k);
    }
}

'use-strict'

process.stdin.resume();
process.stdin.setEncoding('utf8');

let data = '';
let currentLine = 0;

process.stdin.on('data', chunk => {
data += chunk;
});

process.stdin.on('end', () =>{
    data = data.replace(/\s*$/, '')
    .split('\n')
    .map(str => str.replace(/\s*$/, ''));

    let testCases = parseInt(data.shift());

    while(testCases--) {

        const n = data.shift();

        const a = get_ints();

        const res = MY_FUNC(n,a);

        console.log(res)
    }
});

function get_ints() {
    return data.shift().split(' ').map(Number);
}

function readLine() {
        return data[currentLine++];
}

function MY_FUNC(n,a){
  let res = 0;

  for (let i = n - 2; i >= 0; i--) {
    if (a[i + 1] === 0) {
      res = -1
      return res;
    }
    while (a[i] >= a[i + 1]) {
      res++;
      a[i] = Math.floor(a[i] / 2);
    }
  }
    return res;
}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\r');
let currentline = 0;
function readline(){
      return input[currentline++];
}
function alpha() {
  var g = []
  var f = 'abcdefghijklmnopqrstuvwxyz';
  for(var x=0;x<f.length;x++) g[f[x]] = x+1;
  return g;
}
function Alpha() {
  var g = []
  var f = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  for(var x=0;x<f.length;x++) g[f[x]] = x+1;
  return g;
}
var T = readline();
for(var q=1;q<=T;q++) {
  var n = readline();
  var a = readline().split(' ').map((x)=> parseInt(x))
  var prev = a[n-1];
  var v = []
  var ok = true;
  var moves = 0;
  for(var i=n-2;i>=0;i--) {
    var x = a[i];
    while(x >= prev && x > 0) {
      x = Math.floor(x/2);
      moves++;
    }
    if(x >= prev) {
      ok = false;
      break;
    } else {
      prev = x;
    }
  }

  console.log(ok ? moves : -1);
  //fs.appendFileSync('output.txt', (ok?moves:-1)+'\r\n');

  //os.EOL
}
//fs.writeFileSync('output.txt', f.join(' '));

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(n, a) {
  let res = 0;
  for (let i = n - 2; i >= 0; i--) {
    if (a[i + 1] === 0) {
      console.log(-1);
      return;
    }
    while (a[i] >= a[i + 1]) {
      res++;
      a[i] = Math.floor(a[i] / 2);
    }
  }
  console.log(res);
}

async function main(read) {
  let t = Number(await read());
  while (t--) {
    const n = Number(await read());
    const a = (await read()).split(' ').map(Number);
    solve(n, a);
  }
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

const lines = [];
const rl = require('readline').createInterface({ input: process.stdin, output: process.stdout });
rl.on('line', line => lines.push(line));
rl.on('close', main);
let rli = 0;
function readline() {
    return lines[rli++];
}

const ra = ()=>readline().split(' ').map(a=>+a);

function div(val, by){
    return (val - val % by) / by;
}

const cnk = (n, k) => {
    let res = 1;
    for (let i = 1; i <= k; i++)
        res = res * (n - k + i) / i;
    return Math.round(res + 0.01);
}

const sumab = (a, b) => {
    let x = (b - a + 1);
    let y = a + b;
    if (x % 2 === 0) {
        x = Math.round(x / 2);
    } else {
        y = Math.round(y / 2);
    }
    return BigInt(x) * BigInt(y);
}

const calc = (n, a)=>{
    if (n === 1) return 0;
    let result = 0;

    for (let i = n - 2; i >= 0; i--) {
        while (a[i] >= a[i + 1] && a[i] !== 0) {
            a[i] = Math.floor(a[i] / 2);
            result++;
        }
        if (a[i] === 0 && a[i] === a[i + 1]) return -1;
    }

    return result;
};

function main() {
    let T = +readline();
    for (let t = 1; t <= T; t++){
        let n = +readline();
        let a = ra();
        console.log(`${calc(n, a)}`);
    }
}

'use strict';
// HEADER:
const print = console.log;
let IS_MULTITEST, testN;
const pcalc = testN=>{
  let res = calc(testN);
  if (Array.isArray(res)){
    pa(res);
  } else if (Number.isFinite(res) || typeof res=='string'){
    print(res);
  }
};

const main = ()=>{
  if (IS_MULTITEST){
    let T = +readline()
    for (testN=1; testN<=T; testN++)
      pcalc(T);
  } else
   pcalc();
};

const lines = [];
const rl = require('readline').createInterface({
   input: process.stdin, output: process.stdout});
rl.on('line', line=>{
  lines.push(line);
});
rl.on('close', main);
let rli = 0;
const readline = ()=>lines[rli++];
const L = process.env.DEBUG ? console.log : ()=>{};
const LT = obj=>L(`Test #${testN}`, obj);

const ra = ()=>readline().split(' ').map(a=>+a)
const pa = arr=>print(arr.join(' '));

// SOLUTION:
IS_MULTITEST = 1;

const calc = ()=>{
  // READING:
  let [n] = ra();
  let A = ra();
  LT({n, A});
  // PROCESSING:
  let ans = 0;
  for (let i=n-2; i>=0; i--){
    while (A[i]>=A[i+1]){
      if (A[i]==0){
        return -1;
      }
      A[i] = Math.floor(A[i]/2);
      ans++;
    }
  }
  return ans;
};

const readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})
const lines = []
rl.on('line', (input) => {
    lines.push(input);
})
rl.on('close', () => {
// (function() {
    // const lines = require('fs').readFileSync('test.in', 'utf8').split('\n')
    let l = 0;
    let t = +lines[l++]
    const output = []
    for (let i = 0; i < t; i++) {
        const n = +lines[l++]
        const arr = lines[l++].trim().split(' ').map(Number)
        output[i] = solve(n, arr)
    }
    console.log(output.join('\n'))
// })()
})

function solve(n, arr) {
    let ans = 0
    for (let i = arr.length - 2; i >= 0; i--) {
        if (arr[i + 1] === 0) return -1
        while (arr[i] >= arr[i + 1]) {
            arr[i] = Math.floor(arr[i] / 2)
            ans++
        }
    }
    return ans
}

t = parseInt(readline());
for(i = 0; i < t; i++)
{
    var n = readline().split(" ");
    var arr = readline().split(" ");
    var count = 0;
    var k = false;
        for(j = arr.length - 2; j > -1; j--)
        {
            while(arr[j] - arr[j + 1] >= 0)
            {
                if(arr[j + 1] <= j) {
                    k = true;
                    break;
                }
                while(arr[j]/arr[j + 1] > 16385) {
                    arr[j] = arr[j] / 16384;
                    arr[j] -= arr[j] % 1;
                    count += 14;
                }
                arr[j] = arr[j] / 2;
                arr[j] -= arr[j] % 1;
                count++;
            }
            if(k === true) {
                print(-1);
                break;
            }
        }
    if(k === false) print(count);
}

//import { readline, print } from '@ip-algorithmics/codeforces-io';
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var t = +readline();
for (var _i = 0, _a = Array.from(Array(t).keys()); _i < _a.length; _i++) {
    var i = _a[_i];
    readline();
    var a = __spreadArray(__spreadArray([], readline().split(' ').map(function (v) { return +v; }), true), [9999999999], false).reverse();
    var steps = 0;
    for (var _b = 0, _c = Array.from(a.keys()); _b < _c.length; _b++) {
        var i_1 = _c[_b];
        while (a[i_1] >= a[i_1 - 1]) {
            if (a[i_1] === 0) {
                steps = -1;
                break;
            }
            a[i_1] = Math.floor(a[i_1] / 2);
            steps += 1;
        }
    }
    print(steps);
}

/**
 * Author : Fazlul Hoque
 * Node.js version : 12.16.3
 * command : node fileName.js <input.txt >output.txt
 */
"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");
let inputString = ""; //An array of  raw inputs (includes 'n,\r , not usable. need to process before use)
let currentLine = 0; //input sequence controller variable.

process.stdin.on("data", (inputStdin) => {
    inputString += inputStdin;
});
process.stdin.on("end", () => {
    inputString = inputString.split("\n");
    main(); // after input
});

/********** Utility Functions Start **********/
/**
 *  read row Input
 * @returns string
 */
function _readRawLine() {
    return inputString[currentLine++];
}
function readLine(){
    // return _readRawLine().replace(/\s+$/g, '');
    return _readRawLine().replace('\r', '');
}

function print(x) {
    console.log(x);
}

function readSingleInt() {
    const p = readLine();
    return parseInt(p);
}

function readIntArray() {
    return readLine().split(" ").map((v) => parseInt(v));
}

function readArray() {
    return readLine().split(" ");
}

function int(n) {
    return parseInt(n);
}
/********** Utility Functions End **********/
/**
 * Don't Touch this fucntion at all , unless you know what are you doing!
 */
function main() {
    solve();
}

// ********** actual Code Starts from here **********
function isOk(ar)
{
    for(let i=1;i<ar.length ; i++)
    {
        if(ar[i]<=ar[i-1])
        {
            return false;
        }
    }
    return true;
}

function changeIt(ar){
    let cnt = 0;
    for(let i =1;i<ar.length;i++){
        if(ar[i]<=ar[i-1]){
            // console.log("here")
            ar[i-1] = Math.floor(ar[i-1]/2);
            cnt++;
        }
    }
    return cnt;
}

function solve() {
    let t = readSingleInt();
    while(t--)
    {
        let n = readSingleInt();
        let ar = readIntArray();
        let ans =0,gotIt = 0;
        if(n==1){
            gotIt = 1 ,ans = 0;
        }
        // else if(n==2)
        else {
        for(let i =0;i<70;i++){
            // console.log(`\nbefore call ${i}\n`,ar)
            if(!isOk(ar)){
                let d=    changeIt(ar);
                ans += d;
                // console.log(`after call ${i}\n`,ar)
            }
            else{
                gotIt = 1;
                break;
            }
        }
    }
        if(gotIt){
            console.log(ans)
        }else{
            console.log(-1);
        }
    }

}

let readline = require("readline");
let rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false,
});

let lines = 0;
rl.on("line", function (line) {
  if (lines !== 0 && lines % 2 === 0) {
    let input = line.split(" ").map(Number);
    let operations = 0;
    let isIncreasing = true;

    for (
      var index = input.length - 1;
      index >= 0 && isIncreasing && input.length > 1;
      index--
    ) {
      while (input[index] >= input[index + 1] && input[index] !== 0) {
        input[index] = Math.trunc(input[index] / 2);
        operations++;
      }

      if (input[index] >= input[index + 1]) {
        isIncreasing = false;
      }
    }
    if (isIncreasing) {
      console.log(operations);
    } else {
      console.log(-1);
    }
  }
  lines++;
});

/**
 * Author : Fazlul Hoque
 * Node.js version : 12.16.3
 * command : node fileName.js <input.txt >output.txt
 */
"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");
let inputString = ""; //An array of  raw inputs (includes 'n,\r , not usable. need to process before use)
let currentLine = 0; //input sequence controller variable.

process.stdin.on("data", (inputStdin) => {
    inputString += inputStdin;
});
process.stdin.on("end", () => {
    inputString = inputString.split("\n");
    main(); // after input
});

/********** Utility Functions Start **********/
/**
 *  read row Input
 * @returns string
 */
function _readRawLine() {
    return inputString[currentLine++];
}
function readLine(){
    // return _readRawLine().replace(/\s+$/g, '');
    return _readRawLine().replace('\r', '');
}

function print(x) {
    console.log(x);
}

function readSingleInt() {
    const p = readLine();
    return parseInt(p);
}

function readIntArray() {
    return readLine().split(" ").map((v) => parseInt(v));
}

function readArray() {
    return readLine().split(" ");
}

function int(n) {
    return parseInt(n);
}
/********** Utility Functions End **********/
/**
 * Don't Touch this fucntion at all , unless you know what are you doing!
 */
function main() {
    solve();
}

// ********** actual Code Starts from here **********
function isOk(ar)
{
    for(let i=1;i<ar.length ; i++)
    {
        if(ar[i]<=ar[i-1])
        {
            return false;
        }
    }
    return true;
}

function changeIt(ar){
    let cnt = 0;
    for(let i =1;i<ar.length;i++){
        if(ar[i]<=ar[i-1]){
            // console.log("here")
            ar[i-1] = Math.floor(ar[i-1]/2);
            cnt++;
        }
    }
    return cnt;
}

function solve() {
    let t = readSingleInt();
    while(t--)
    {
        let n = readSingleInt();
        let ar = readIntArray();
        let ans =0,gotIt = 0;
        if(n==1){
            gotIt = 1 ,ans = 0;
        }
        // else if(n==2)
        else {
        for(let i =0;i<35;i++){
            // console.log(`\nbefore call ${i}\n`,ar)
            if(!isOk(ar)){
                let d=    changeIt(ar);
                ans += d;
                // console.log(`after call ${i}\n`,ar)
            }
            else{
                gotIt = 1;
                break;
            }
        }
    }
        if(gotIt){
            console.log(ans)
        }else{
            console.log(-1);
        }
    }

}

/**
 * Author : Fazlul Hoque
 * Node.js version : 12.16.3
 * command : node fileName.js <input.txt >output.txt
 */
"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");
let inputString = ""; //An array of  raw inputs (includes 'n,\r , not usable. need to process before use)
let currentLine = 0; //input sequence controller variable.

process.stdin.on("data", (inputStdin) => {
    inputString += inputStdin;
});
process.stdin.on("end", () => {
    inputString = inputString.split("\n");
    main(); // after input
});

/********** Utility Functions Start **********/
/**
 *  read row Input
 * @returns string
 */
function _readRawLine() {
    return inputString[currentLine++];
}
function readLine(){
    // return _readRawLine().replace(/\s+$/g, '');
    return _readRawLine().replace('\r', '');
}

function print(x) {
    console.log(x);
}

function readSingleInt() {
    const p = readLine();
    return parseInt(p);
}

function readIntArray() {
    return readLine().split(" ").map((v) => parseInt(v));
}

function readArray() {
    return readLine().split(" ");
}

function int(n) {
    return parseInt(n);
}
/********** Utility Functions End **********/
/**
 * Don't Touch this fucntion at all , unless you know what are you doing!
 */
function main() {
    solve();
}

// ********** actual Code Starts from here **********
function isOk(ar)
{
    for(let i=1;i<ar.length ; i++)
    {
        if(ar[i]<=ar[i-1])
        {
            return false;
        }
    }
    return true;
}

function changeIt(ar){
    let cnt = 0;
    for(let i =1;i<ar.length;i++){
        if(ar[i]<=ar[i-1]){
            // console.log("here")
            ar[i-1] = Math.floor(ar[i-1]/2);
            cnt++;
        }
    }
    return cnt;
}

function solve() {
    let t = readSingleInt();
    while(t--)
    {
        let n = readSingleInt();
        let ar = readIntArray();
        let ans =0,gotIt = 0;
        if(n==1){
            gotIt = 1 ,ans = 0;
        }
        // else if(n==2)
        else {
        for(let i =0;i<33;i++){
            // console.log(`\nbefore call ${i}\n`,ar)
            if(!isOk(ar)){
                let d=    changeIt(ar);
                ans += d;
                // console.log(`after call ${i}\n`,ar)
            }
            else{
                gotIt = 1;
                break;
            }
        }
    }
        if(gotIt){
            console.log(ans)
        }else{
            console.log(-1);
        }
    }

}

let readline = require("readline");
let rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false,
});

let lines = 0;
rl.on("line", function (line) {
  if (lines !== 0 && lines % 2 === 0) {
    let input = line.split(" ").map(Number);
    let operations = 0;
    let isIncreasing = true;

    if (input.length === 1) {
      return console.log(operations);
    }

    for (var index = input.length - 1; index >= 0 && isIncreasing; index--) {
      while (input[index] >= input[index + 1] && input[index] !== 0) {
        input[index] = Math.trunc(input[index] / 2);
        operations++;
      }

      if (input[index] >= input[index + 1]) {
        isIncreasing = false;
      }
    }
    if (isIncreasing) {
      console.log(operations + "");
    } else {
      console.log("-1");
    }
  }
  lines++;
});

let readline = require("readline");
let rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false,
});

let lines = 0;
rl.on("line", function (line) {
  if (lines !== 0 && lines % 2 === 0) {
    let input = line.split(" ").map(Number);
    let operations = 0;
    let isIncreasing = true;

    if (input.length === 1) {
      return console.log(operations);
    }

    for (var index = input.length - 1; index >= 0 && isIncreasing; index--) {
      while (input[index] >= input[index + 1] && input[index] !== 0) {
        input[index] = Math.trunc(input[index] / 2);
        operations++;
      }

      if (input[index] >= input[index + 1]) {
        isIncreasing = false;
      }
    }
    if (isIncreasing) {
      console.log(operations);
    } else {
      console.log("-1");
    }
  }
  lines++;
});


function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = Math.min(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == min : v == min);
  return [this[i], i, min];
}
Array.prototype.max = function (fn) {
  let max = Math.max(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == max : v == max);
  return [this[i], i, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $()
  let a = $(n)
  let f = Arr(0, n - 1, 1, i => i);
  let imin = Array(n); imin[0] = -1;
  let imax = Array(n); imax[0] = -1;
  let j = 0;
  For(1, n - 1, 1, i => {
    imax[i] = i - 1;
    while (imax[i] >= 0 && a[imax[i]] <= a[i]) imax[i] = imax[imax[i]];
    imin[i] = i - 1;
    while (imin[i] >= 0 && a[imin[i]] >= a[i]) imin[i] = imin[imin[i]];

    f[i] = f[i - 1] + 1;
    if (a[i] < a[i - 1]) {
      j = imin[i - 1];
      while (j >= 0) {
        f[i] = min(f[i], f[j] + 1);
        if (a[j] <= a[i]) break;
        j = imin[j];
      }
    } else if (a[i] > a[i - 1]) {
      j = imax[i - 1];
      while (j >= 0) {
        f[i] = min(f[i], f[j] + 1);
        if (a[j] >= a[i]) break;
        j = imax[j];
      }
    }
  })
  log(f[n - 1]);
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    D();
});

function readline() {
    return inputString[currentLine++];
}

function ti(a){
    for(let i = 0; i < a.length; i++)
        a[i] = parseInt(a[i]);

    return a;
}

function pi(x){
    return parseInt(x);
}

function D(){
    let n = pi(readline());

    let h = ti(readline().split(' '));
    let dp = new Array(n);
    dp.fill(99999999999999999);
    dp[0] = 0;
    let st = [];
    let lse = new Array(n);
    let lge = new Array(n);
    let rse = new Array(n);
    let rge = new Array(n);

    for(let i = 0; i < n; i++){
        while(st.length > 0 && h[st[st.length-1]] > h[i])
            st.pop();

        if(st.length === 0)
            lse[i] = -1;
        else
            lse[i] = st[st.length-1];

        st.push(i);
    }

    st = [];
    for(let i = 0; i < n; i++){
        while(st.length > 0 && h[st[st.length-1]] < h[i])
            st.pop();

        if(st.length === 0)
            lge[i] = -1;
        else
            lge[i] = st[st.length-1];

        st.push(i);
    }

    st = [];
    for(let i = n-1; i >= 0; i--){
        while(st.length > 0 && h[st[st.length-1]] > h[i])
            st.pop();

        if(st.length === 0)
            rse[i] = -1;
        else
            rse[i] = st[st.length-1];

        st.push(i);
    }

    st = [];
    for(let i = n-1; i >= 0; i--){
        while(st.length > 0 && h[st[st.length-1]] < h[i])
            st.pop();

        if(st.length === 0)
            rge[i] = -1;
        else
            rge[i] = st[st.length-1];

        st.push(i);
    }

    let jumps = new Array(n);
    for(let i = 0; i < n; i++)
        jumps[i] = [];

    for(let i = 0; i < n; i++){
        if(rse[i] !== -1)
            jumps[rse[i]].push(i);

        if(rge[i] !== -1)
            jumps[rge[i]].push(i);

        if(lse[i] !== -1)
            jumps[i].push(lse[i]);

        if(lge[i] !== -1)
            jumps[i].push(lge[i]);
    }

    for(let i = 1; i < n; i++){
        for(let j of jumps[i])
            dp[i] = Math.min(dp[i], dp[j] + 1);
    }

    //console.log(dp);
    console.log(dp[n-1]);
}

function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = Math.min(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == min : v == min);
  return [this[i], i, min];
}
Array.prototype.max = function (fn) {
  let max = Math.max(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == max : v == max);
  return [this[i], i, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $()
  let a = $(n)
  let f = Arr(0, n - 1, 1, i => i);
  let j = 0;
  For(1, n - 1, 1, i => {
    f[i] = f[i - 1] + 1;
    if (a[i] < a[i - 1]) {
      if (a[i - 1] >= a[i - 2]) j = i - 2;
      j = For(j, 0, -1, j => a[j] < a[i - 1] ? j : null);
      while (j >= 0 && a[j] < a[j + 1] && a[j + 1] > a[i]) {
        f[i] = min(f[i], f[j--] + 1);
      }
      j++;
    } else if (a[i] > a[i - 1]) {
      if (a[i - 1] <= a[i - 2]) j = i - 2;
      j = For(j, 0, -1, j => a[j] > a[i - 1] ? j : null);
      while (j >= 0 && a[j] > a[j + 1] && a[j + 1] < a[i]) {
        f[i] = min(f[i], f[j--] + 1);
      }
      j++;
    }
  })
  log(f[n - 1]);
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    D();
});

function readline() {
    return inputString[currentLine++];
}

function pi(a) {
    return parseInt(a);
}

function ti(a){
    for(let i = 0; i < a.length; i++)
        a[i] = pi(a[i]);

    return a;
}

function D(){
    let n = pi(readline());

    let a = ti(readline().split(' '));
    let dp = new Array(n);
    dp[0] = 0;
    for(let i = 1; i < n; i++){
        let min = 99999999999999999999;
        let max = 0;

        for(let j = i-1; j >= 0; j--){
            if(j === i-1){
                dp[i] = dp[j] + 1;
            }else{
                min = Math.min(min, a[j+1]);
                max = Math.max(max, a[j+1]);
                if(max < Math.min(a[i], a[j])){
                    dp[i] = Math.min(dp[i], dp[j] + 1);
                }else if(min > Math.max(a[i], a[j])){
                    dp[i] = Math.min(dp[i], dp[j] + 1);
                }
            }
        }
    }

    console.log(dp);
    console.log(dp[n-1]);
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    D();
});

function readline() {
    return inputString[currentLine++];
}

function pi(a) {
    return parseInt(a);
}

function ti(a){
    for(let i = 0; i < a.length; i++)
        a[i] = pi(a[i]);

    return a;
}

function D(){
    let n = pi(readline());

    let a = ti(readline().split(' '));
    let dp = new Array(n);
    dp.fill(99999999999999999);
    dp[0] = 0;
    let stMin = [0];
    let stMax = [0];
    for(let i = 1; i < n; i++){
        while(stMin.length > 0 && a[stMin[stMin.length-1]] > a[i]){
            stMin.pop();
        }

        while(stMax.length > 0 && a[stMax[stMax.length-1]] < a[i]){
            stMax.pop();
        }

        if(stMin.length > 0 || stMax.length > 0){
            if(stMin.length > 0)
                dp[i] = dp[stMin[stMin.length-1]] + 1;

            if(stMax.length > 0)
                dp[i] = Math.min(dp[i], dp[stMax[stMax.length-1]] + 1);
        }else{
            dp[i] = dp[i-1] + 1;
        }

        stMin.push(i);
        stMax.push(i);
    }
    //console.log(dp);
    console.log(dp[n-1]);
}