Today's morning was exceptionally snowy. Meshanya decided to go outside and noticed a huge snowball rolling down the mountain! Luckily, there are two stones on that mountain.Initially, snowball is at height $$$h$$$ and it has weight $$$w$$$. Each second the following sequence of events happens: snowball's weights increases by $$$i$$$, where $$$i$$$ — is the current height of snowball, then snowball hits the stone (if it's present at the current height), then snowball moves one meter down. If the snowball reaches height zero, it stops.There are exactly two stones on the mountain. First stone has weight $$$u_1$$$ and is located at height $$$d_1$$$, the second one — $$$u_2$$$ and $$$d_2$$$ respectively. When the snowball hits either of two stones, it loses weight equal to the weight of that stone. If after this snowball has negative weight, then its weight becomes zero, but the snowball continues moving as before.  Find the weight of the snowball when it stops moving, that is, it reaches height 0.

First line contains two integers $$$w$$$ and $$$h$$$ — initial weight and height of the snowball ($$$0 \le w \le 100$$$; $$$1 \le h \le 100$$$). Second line contains two integers $$$u_1$$$ and $$$d_1$$$ — weight and height of the first stone ($$$0 \le u_1 \le 100$$$; $$$1 \le d_1 \le h$$$). Third line contains two integers $$$u_2$$$ and $$$d_2$$$ — weight and heigth of the second stone ($$$0 \le u_2 \le 100$$$; $$$1 \le d_2 \le h$$$; $$$d_1 \ne d_2$$$). Notice that stones always have different heights.

Output a single integer — final weight of the snowball after it reaches height 0.

NoteIn the first example, initially a snowball of weight 4 is located at a height of 3, there are two stones of weight 1, at a height of 1 and 2, respectively. The following events occur sequentially:   The weight of the snowball increases by 3 (current height), becomes equal to 7.  The snowball moves one meter down, the current height becomes equal to 2.  The weight of the snowball increases by 2 (current height), becomes equal to 9.  The snowball hits the stone, its weight decreases by 1 (the weight of the stone), becomes equal to 8.  The snowball moves one meter down, the current height becomes equal to 1.  The weight of the snowball increases by 1 (current height), becomes equal to 9.  The snowball hits the stone, its weight decreases by 1 (the weight of the stone), becomes equal to 8.  The snowball moves one meter down, the current height becomes equal to 0. Thus, at the end the weight of the snowball is equal to 8.

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

let l = 0;
let w = []
let h = []

rl.on('line', (input) => {
    if (l === 0) {
        let inp = input.split(" ").map(x => parseInt(x))
        w.push(inp[0])
        h.push(inp[1])
    } else if (l === 1) {
        let inp = input.split(" ").map(x => parseInt(x))
        w.push(inp[0])
        h.push(inp[1])
    } else {
        rl.close();
        let inp = input.split(" ").map(x => parseInt(x))
        let wg = w[0]
        let hg = h[0]
        w.push(inp[0])
        h.push(inp[1])
        if (h[1] < h[2]) {
            let aux = h[1]
            h[1] = h[2]
            h[2] = aux
            aux = w[1]
            w[1] = w[2]
            w[2] = aux
        }
        for (let i = h[0]; i >= h[1]; i--) {
            wg += i
            hg--
        }

        wg -= w[1]
        if (wg < 0)
            wg = 0
        if (hg <= 0) {
            console.log(wg)
            return
        }
        for (let i = h[1] - 1; i >= h[2]; i--) {
            wg += i
            hg--
        }
        wg -= w[2]
        if (wg < 0)
            wg = 0
        for (let i = hg; i >= 0; i--) {
            wg += i;
        }
        console.log(wg)
    }
    l++
});

process.stdin.setEncoding('utf8');
let input = [];
process.stdin.on('readable', () => {
    const chunk = process.stdin.read();
    if (chunk !== null) {
        let re = /\n\s*| /;
        input = chunk.split(re).slice(0, -1);
        let [w, h, u1, d1, u2, d2] = input.map(value => parseInt(value));
        for (; h >= 0; h--) {
            w += h;
            if (h == d1) w -= u1;
            if (h == d2) w -= u2;
            w = Math.max(w, 0);
        }
        console.log(w);
        process.exit();
    }
});

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
let input = [];
rl.on('line', (inputString) => {
    let [x, y] = inputString.split(' ').map(v => parseInt(v));
    input = input.concat([x, y]);
    if (input.length == 6) {
        let [w, h, u1, d1, u2, d2] = input;
        for (; h >= 0; h--) {
            w += h;
            if (h == d1) w -= u1;
            if (h == d2) w -= u2;
            if (w < 0) w = 0;
        }
        console.log(w);
        process.exit();
    }
});

process.stdin.setEncoding('utf8');
let input = [];
process.stdin.on('readable', () => {
    const chunk = process.stdin.read();
    if (chunk !== null) {
        let re = /\n\s*| /;
        input = chunk.split(re).slice(0, 6);
        console.log(snowball.apply(null, input.map(value => parseInt(value))));
        process.exit(0);
    }
});

function snowball(weight, height, u1, d1, u2, d2) {
    let w = weight, h = height;
    for (; h >= 0; h--) {
        w += h;
        if (h == d1) w = Math.max(w - u1, 0);
        if (h == d2) w = Math.max(w - u2, 0);
    }
    return w;
}

process.stdin.setEncoding('utf8');
let input = [];
process.stdin.on('readable', () => {
    const chunk = process.stdin.read();
    if (chunk !== null) {
        let re = /\n\s*| /;
        input = chunk.split(re).slice(0, -1);
        let myinput = input.map(value => parseInt(value));
        console.log(snowball.apply(null, myinput));
        process.exit(0);
    }
});

function snowball(weight, height, u1, d1, u2, d2) {
    let w = weight, h = height;
    for (; h >= 0; h--) {
        w += h;
        if (h == d1) w -= u1;
        if (h == d2) w -= u2;
        if (w < 0) w = 0;
    }
    return w;
}

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
let input = [];
rl.on('line', (inputString) => {
    let [x, y] = inputString.split(' ').map(v => parseInt(v));
    input = input.concat([x, y]);
    if (input.length == 6) {
        let [w, h, u1, d1, u2, d2] = input;
        for (; h >= 0; h--) {
            w += h;
            if (h == d1) w -= u1;
            if (h == d2) w -= u2;
            w = Math.max(w, 0);
        }
        console.log(w);
        process.exit();
    }
});

process.stdin.setEncoding('utf8');
process.stdin.on('readable', () => {
    const chunk = process.stdin.read();
    if (chunk !== null) {
        let re = /\n\s*| /;
        let input = chunk.split(re).slice(0, -1);
        console.log(snowball.apply(null, input.map(value => parseInt(value))));
        process.exit(0);
    }
});

function snowball(weight, height, u1, d1, u2, d2) {
    let w = weight, h = height;
    for (; h >= 0; h--) {
        w += h;
        if (h == d1) w = Math.max(w - u1, 0);
        if (h == d2) w = Math.max(w - u2, 0);
    }
    return w;
}

process.stdin.setEncoding('utf8');
process.stdin.on('readable', () => {
    const chunk = process.stdin.read();
    if (chunk !== null) {
        let re = /\n\s*| /;
        let input = chunk.split(re).slice(0, -1);
        console.log(snowball.apply(null, input.map(value => parseInt(value))));
        process.exit(0);
    }
});

function snowball(w, h, u1, d1, u2, d2) {
    for (; h >= 0; h--) {
        w += h;
        if (h == d1) w -= u1;
        if (h == d2) w -= u2;
        w = Math.max(w, 0);
    }
    return w;
}

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
rl.on('line', (inputString) => {
    let [x, y] = inputString.split(' ').map(v => parseInt(v));
    if (!rl.on.input) rl.on.input = [];
    rl.on.input = rl.on.input.concat([x, y]);
    if (rl.on.input.length == 6) {
        sb.apply(null, rl.on.input);
        process.exit();
    }
});

function sb(w, h, u1, d1, u2, d2) {
    for (; h >= 0; h--) {
        w += h;
        if (h == d1) w -= u1;
        if (h == d2) w -= u2;
        w = Math.max(0, w);
    }
    console.log(w);
}

process.stdin.setEncoding('utf8');
let input = [];
process.stdin.on('readable', () => {
    const chunk = process.stdin.read();
    if (chunk !== null) {
        let re = /\n\s*| /;
        input = chunk.split(re).slice(0, -1);
        let myinput = input.map(value => parseInt(value));
        console.log(snowball.apply(null, myinput));
        process.exit(0);
    }
});

function snowball(weight, height, u1, d1, u2, d2) {
    let w = weight, h = height;
    for (; h >= 0; h--) {
        w += h;
        if (h == d1) w = (w - u1) > 0 ? (w - u1) : 0;
        if (h == d2) w = (w - u2) > 0 ? (w - u2) : 0;
    }
    return w;
}

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
let input = [];
rl.on('line', (inputString) => {
    input = input.concat(inputString.split(' '));
    if (input.length == 6) {
        let [w, h, u1, d1, u2, d2] = input.map(v => parseInt(v));
        for (; h >= 0; h--) {
            w += h;
            if (h == d1) w -= u1;
            if (h == d2) w -= u2;
            w = Math.max(w, 0);
        }
        console.log(w);
        process.exit();
    }
});

const readline = require('readline');

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

function readLines(number, cb) {
    const lines = [];
    rl.on('line', function(line) {
        lines.push(line);
        if (lines.length === number) {
            rl.close();
            cb(lines);
        }
    });
};

function readLines(number, cb) {
    const lines = [];
    rl.on('line', function(line) {
        lines.push(line);
        if (lines.length === number) {
            rl.close();
            cb(lines);
        }
    });
};

function doIt(lines) {
    const snowBall = lines[0].split(" ").map(x => parseInt(x));
    const stoneOne = lines[1].split(" ").map(x => parseInt(x));
    const stoneTwo = lines[2].split(" ").map(x => parseInt(x));

    let snowW = snowBall[0];
    let snowH = snowBall[1];

    for (let i = snowH; i>=0; i--) {
        snowW = snowW+i;
        if (i === stoneOne[1]) {
            snowW -= stoneOne[0];
        }

        if (i === stoneTwo[1]) {
            snowW -= stoneTwo[0];
        }
        if (snowW < 0)
            snowW = 0;
    }
    console.log(snowW);
}

readLines(3, doIt);

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = [];
read.on('line', function(input){inLine.push(input);});
read.on('close', function(){
	obj = init(inLine);
	console.error('\n↑入力 ↓出力');
	Main();
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);}
function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();}
function init(input){
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}}
	};
}
function myout(s){console.log(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
  var one = nextIntArray();
  var w = one[0];
  var h = one[1];
  var two = nextIntArray();
  var three = nextIntArray();
  var u1 = two[0];
  var d1 = two[1];
  var u2 = three[0];
  var d2 = three[1];
  for(var i = h; i >= 0; i--){
    w += i;
    if(i == d1){
      w -= u1;
    }
    if(i == d2){
      w -= u2;
    }
    if(w < 0){
      w = 0;
    }
  }
  myout(w);
}

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;
let w, h;
let u1, u2, d1, d2;

rl.on('line', (d) => {
  if (c === 0) {
    c++;
    [w, h] = d.split(' ').map(Number);
    return;
  }

  if (c === 1) {
    c++;
    [u1, d1] = d.split(' ').map(Number);
    return;
  }

  [u2, d2] = d.split(' ').map(Number);

  c++;
});

rl.on('close', () => {
  let ans = w;

  while (h > 0) {
    ans += h;

    if (h === d1) {
      ans -= u1;
    }

    if (h === d2) {
      ans -= u2;
    }

    if (ans < 0) {
      ans = 0;
    }

    h--;
  }

  console.log(Math.max(0, ans));
});

var ar1=readline().split(" ");
var ar2=readline().split(" ");
var ar3=readline().split(" ");
var w=parseInt(ar1[0]),h=parseInt(ar1[1]);
var w1=parseInt(ar2[0]),h1=parseInt(ar2[1]);
var w2=parseInt(ar3[0]),h2=parseInt(ar3[1]);
while(h!==0){
	w+=h;
	if(h===h1){w-=w1;}
	else if(h===h2){w-=w2;}
	h--;
	if(w<0){w=0;}
	if(h===0){
		if(h===h1){w-=w1;}
	else if(h===h2){w-=w2;}
	}
}print(w);

process.stdin.setEncoding('utf8');
let input = [];
process.stdin.on('readable', () => {
    const chunk = process.stdin.read();
    if (chunk !== null) {
        let re = /\n\s*| /;
        input = chunk.split(re).slice(0, -1);
        let myinput = input.map(value => parseInt(value));
        console.log(snowball.apply(null, myinput));
        process.exit(0);
    }
});

function snowball(weight, height, u1, d1, u2, d2) {
    let w = weight, h = height;
    for (; h >= 0; h--) {
        w += h;
        if (h == d1) w -= u1;
        if (h == d2) w -= u2;
        if (!w) w = 0;
    }
    return w;
}

process.stdin.setEncoding('utf8');
let input = [];
process.stdin.on('readable', () => {
    const chunk = process.stdin.read();
    if (chunk !== null) {
        input = input.concat(chunk.slice(0, -1).split(' '));
        if (input.length == 6) {
            let myinput = input.map(value => parseInt(value));
            console.log(snowball.apply(null, myinput));
            //process.exit(0);
        }
    }
});

function snowball(weight, height, u1, d1, u2, d2) {
    let w = weight, h = height;
    for (; h >= 0; h--) {
        w += h;
        if (h == d1) w = w - u1 ? w - u1 : 0;
        if (h == d2) w = w - u2 ? w - u2 : 0;
    }
    return w;
}

process.stdin.setEncoding('utf8');
let input = [];
process.stdin.on('readable', () => {
    const chunk = process.stdin.read();
    if (chunk !== null) {
        let re = /\n\s*| /;
        input = chunk.split(re).slice(0, -1);
        let myinput = input.map(value => parseInt(value));
        console.log(input);
        console.log(snowball.apply(null, myinput));
        process.exit(0);
    }
});

function snowball(weight, height, u1, d1, u2, d2) {
    let w = weight, h = height;
    for (; h >= 0; h--) {
        w += h;
        if (h == d1) w = (w - u1) > 0 ? (w - u1) : 0;
        if (h == d2) w = (w - u2) > 0 ? (w - u2) : 0;
    }
    return w;
}

process.stdin.setEncoding('utf8');
let input = [];
process.stdin.on('readable', () => {
    const chunk = process.stdin.read();
    if (chunk !== null) {
        let re = /\n| /;
        input = chunk.split(re).slice(0, -1);
        let myinput = input.map(value => parseInt(value));
        console.log(snowball.apply(null, myinput));
        process.exit(0);
    }
});

function snowball(weight, height, u1, d1, u2, d2) {
    let w = weight, h = height;
    for (; h >= 0; h--) {
        w += h;
        if (h == d1) w = w - u1 ? w - u1 : 0;
        if (h == d2) w = w - u2 ? w - u2 : 0;
    }
    return w;
}

process.stdin.setEncoding('utf8');
let input = [];
process.stdin.on('readable', () => {
    const chunk = process.stdin.read();
    if (chunk !== null) {
        input = input.concat(chunk.slice(0, -1).split(' '));
        if (input.length == 6) {
            let myinput = input.map(value => parseInt(value));
            console.log(snowball.apply(null, myinput));
            process.exit(0);
        }
    }
});

function snowball(weight, height, u1, d1, u2, d2) {
    let w = weight, h = height;
    for (; h >= 0; h--) {
        w += h;
        if (h == d1) w = w - u1 ? w - u1 : 0;
        if (h == d2) w = w - u2 ? w - u2 : 0;
    }
    return w;
}

process.stdin.setEncoding('utf8');
let input = [];
process.stdin.on('readable', () => {
    const chunk = process.stdin.read();
    if (chunk !== null) {
        let re = /\n\s*| /;
        input = chunk.split(re).slice(0, -1);
        let myinput = input.map(value => parseInt(value));
        process.exit(0);
    }
});

function snowball(weight, height, u1, d1, u2, d2) {
    let w = weight, h = height;
    for (; h >= 0; h--) {
        w += h;
        if (h == d1) w = (w - u1) > 0 ? (w - u1) : 0;
        if (h == d2) w = (w - u2) > 0 ? (w - u2) : 0;
    }
    return w;
}

function snowball(weight, height, u1, d1, u2, d2) {
    let w = weight, h = height;
    for (; h >= 0; h--) {
        w = w + h;
        if (h == d2) {
            w = w - u2 ? w - u2 : 0;
        }
        if (h == d1) {
            w = w - u1 ? w - u1 : 0;
        }
    }
    return w;
}

[w, h, u1, d1, u2, d2] = process.argv.slice(2).map((value) => parseInt(value));
weight = snowball(w, h, u1, d1, u2, d2);
console.log(weight);

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;
let w, h;
let u1, u2, d1, d2;

rl.on('line', (d) => {
  if (c === 0) {
    c++;
    [w, h] = d.split(' ').map(Number);
    return;
  }

  if (c === 1) {
    c++;
    [u1, d1] = d.split(' ').map(Number);
    return;
  }

  [u2, d2] = d.split(' ').map(Number);

  c++;
});

rl.on('close', () => {
  let ans = w;

  ans += h*(h+1) / 2;
  ans -= u1;
  ans -= u2;
  ans = Math.max(0, ans);

  console.log(ans);
});

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

let l = 0;
let w = []
let h = []

rl.on('line', (input) => {
    if (l === 0) {
        let inp = input.split(" ").map(x => parseInt(x))
        w.push(inp[0])
        h.push(inp[1])
    } else if (l === 1) {
        let inp = input.split(" ").map(x => parseInt(x))
        w.push(inp[0])
        h.push(inp[1])
    } else {
        rl.close();
        let inp = input.split(" ").map(x => parseInt(x))
        let wg = w[0]
        let hg = h[0]
        w.push(inp[0])
        h.push(inp[1])
        if (h[1] > h[2]) {
            for (let i = h[0]; i >= h[1]; i--) {
                wg += i
                hg--
            }
            wg -= w[1]
            if (hg <= 0) {
                if (wg < 0)
                    wg = 0
                console.log(wg)
                return
            }
            for (let i = h[1] - 1; i >= h[2]; i--) {
                wg += i
                hg--
            }
            wg -= w[2]
            if (hg > 0)
                wg += hg
            if (wg < 0)
                wg = 0
            console.log(wg)
        } else {
            for (let i = h[0]; i >= h[2]; i--) {
                wg += i
                hg--
            }
            wg -= w[2]

            if (hg <= 0) {
                if (wg < 0)
                    wg = 0
                console.log(wg)
                return
            }

            for (let i = h[2] - 1; i >= h[1]; i--) {
                wg += i
                hg--
            }
            wg -= w[1]
            if (hg > 0)
                wg += hg
            if (wg < 0)
                wg = 0
            console.log(wg)
        }
    }
    l++
});

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

let l = 0;
let w = []
let h = []

rl.on('line', (input) => {
    if (l === 0) {
        let inp = input.split(" ").map(x => parseInt(x))
        w.push(inp[0])
        h.push(inp[1])
    } else if (l === 1) {
        let inp = input.split(" ").map(x => parseInt(x))
        w.push(inp[0])
        h.push(inp[1])
    } else {
        rl.close();
        let inp = input.split(" ").map(x => parseInt(x))
        let wg = w[0]
        let hg = h[0]
        w.push(inp[0])
        h.push(inp[1])
        if (h[1] > h[2]) {
            hg++
            for (let i = h[0]; i >= h[1]; i--) {
                wg += i
                hg--
            }
            wg -= w[1]
            if (hg <= 0) {
                if (wg < 0)
                    wg = 0
                console.log(wg)
                return
            }
            hg--
            for (let i = h[1] - 1; i >= h[2]; i--) {
                wg += i
                hg--
            }
            wg -= w[2]
            if (hg > 0)
                wg += hg
            if (wg < 0)
                wg = 0
            console.log(wg)
        } else {
            hg++
            for (let i = h[0]; i >= h[2]; i--) {
                wg += i
                hg--
            }
            wg -= w[2]

            if (hg <= 0) {
                if (wg < 0)
                    wg = 0
                console.log(wg)
                return
            }

            for (let i = h[2] - 1; i >= h[1]; i--) {
                wg += i
                hg--
            }
            wg -= w[1]
            if (hg > 0)
                wg += hg
            if (wg < 0)
                wg = 0
            console.log(wg)
        }
    }
    l++
});

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

let l = 0;
let w = []
let h = []

rl.on('line', (input) => {
    if (l === 0) {
        let inp = input.split(" ").map(x => parseInt(x))
        w.push(inp[0])
        h.push(inp[1])
    } else if (l === 1) {
        let inp = input.split(" ").map(x => parseInt(x))
        w.push(inp[0])
        h.push(inp[1])
    } else {
        rl.close();
        let inp = input.split(" ").map(x => parseInt(x))
        let wg = w[0]
        let hg = h[0]
        w.push(inp[0])
        h.push(inp[1])
        if (h[1] > h[2]) {
            for (let i = h[0]; i >= h[1]; i--) {
                wg += i
                hg--
            }
            wg -= w[1]
            if (wg < 0)
                wg = 0
            if (hg <= 0) {
                console.log(wg)
                return
            }
            for (let i = h[1] - 1; i >= h[2]; i--) {
                wg += i
                hg--
            }
            wg -= w[2]
            if (wg < 0)
                wg = 0

            if (hg > 0)
                wg += hg
            console.log(wg)
        } else {
            for (let i = h[0]; i >= h[2]; i--) {
                wg += i
                hg--
            }
            wg -= w[2]
            if (wg < 0)
                wg = 0
            if (hg <= 0) {
                console.log(wg)
                return
            }
            for (let i = h[2] - 1; i >= h[1]; i--) {
                wg += i
                hg--
            }
            wg -= w[1]
            if (wg < 0)
                wg = 0
            if (hg > 0)
                wg += hg
            console.log(wg)
        }
    }
    l++
});

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

let l = 0;
let w = []
let h = []

rl.on('line', (input) => {
    if (l === 0) {
        let inp = input.split(" ").map(x => parseInt(x))
        w.push(inp[0])
        h.push(inp[1])
    } else if (l === 1) {
        let inp = input.split(" ").map(x => parseInt(x))
        w.push(inp[0])
        h.push(inp[1])
    } else {
        rl.close();
        let inp = input.split(" ").map(x => parseInt(x))
        let wg = w[0]
        let hg = h[0]
        w.push(inp[0])
        h.push(inp[1])
        if (h[1] > h[2]) {
            hg++
            for (let i = h[0]; i >= h[1]; i--) {
                wg += i
                hg--
            }
            wg -= w[1]
            if (hg <= 0) {
                if (wg < 0)
                    wg = 0
                console.log(wg)
                return
            }

            for (let i = h[1] - 1; i >= h[2]; i--) {
                wg += i
                hg--
            }
            wg -= w[2]
            if (wg < 0)
                wg = 0
            console.log(wg)
        } else {
            hg++
            for (let i = h[0]; i >= h[2]; i--) {
                wg += i
                hg--
            }
            wg -= w[2]

            if (hg <= 0) {
                if (wg < 0)
                    wg = 0
                console.log(wg)
                return
            }

            for (let i = h[2] - 1; i >= h[1]; i--) {
                wg += i
                hg--
            }
            wg -= w[1]
            if (wg < 0)
                wg = 0
            console.log(wg)
        }
    }
    l++
});

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

let l = 0;
let w = []
let h = []

rl.on('line', (input) => {
    if (l === 0) {
        let inp = input.split(" ").map(x => parseInt(x))
        w.push(inp[0])
        h.push(inp[1])
    } else if (l === 1) {
        let inp = input.split(" ").map(x => parseInt(x))
        w.push(inp[0])
        h.push(inp[1])
    } else {
        rl.close();
        let inp = input.split(" ").map(x => parseInt(x))
        let wg = w[0]
        let hg = h[0]
        w.push(inp[0])
        h.push(inp[1])
        if (h[1] > h[2]) {
            hg++
            for (let i = h[0]; i >= h[1]; i--) {
                wg += i
                hg--
            }
            wg -= w[1]
            if (hg <= 0) {
                console.log(wg)
                return
            }

            for (let i = h[1] - 1; i >= h[2]; i--) {
                wg += i
                hg--
            }
            wg -= w[2]
            console.log(wg)
        } else {
            hg++
            for (let i = h[0]; i >= h[2]; i--) {
                wg += i
                hg--
            }
            wg -= w[2]

            if (hg <= 0) {
                console.log(wg)
                return
            }

            for (let i = h[2] - 1; i >= h[1]; i--) {
                wg += i
                hg--
            }
            wg -= w[1]
            console.log(wg)
        }
    }
    l++
});


Petya and Vasya decided to play a game. They have n cards (n is an even number). A single integer is written on each card.Before the game Petya will choose an integer and after that Vasya will choose another integer (different from the number that Petya chose). During the game each player takes all the cards with number he chose. For example, if Petya chose number 5 before the game he will take all cards on which 5 is written and if Vasya chose number 10 before the game he will take all cards on which 10 is written.The game is considered fair if Petya and Vasya can take all n cards, and the number of cards each player gets is the same.Determine whether Petya and Vasya can choose integer numbers before the game so that the game is fair.

The first line contains a single integer n (2 ≤ n ≤ 100) — number of cards. It is guaranteed that n is an even number. The following n lines contain a sequence of integers a1, a2, ..., an (one integer per line, 1 ≤ ai ≤ 100) — numbers written on the n cards.

If it is impossible for Petya and Vasya to choose numbers in such a way that the game will be fair, print "NO" (without quotes) in the first line. In this case you should not print anything more. In the other case print "YES" (without quotes) in the first line. In the second line print two distinct integers — number that Petya should choose and the number that Vasya should choose to make the game fair. If there are several solutions, print any of them.

NoteIn the first example the game will be fair if, for example, Petya chooses number 11, and Vasya chooses number 27. Then the will take all cards — Petya will take cards 1 and 4, and Vasya will take cards 2 and 3. Thus, each of them will take exactly two cards.In the second example fair game is impossible because the numbers written on the cards are equal, but the numbers that Petya and Vasya should choose should be distinct.In the third example it is impossible to take all cards. Petya and Vasya can take at most five cards — for example, Petya can choose number 10 and Vasya can choose number 20. But for the game to be fair it is necessary to take 6 cards.

(function() {

var line = Number(readline());
var numbers = {};
for (var i = 0; i < line; i++) {
  var next = Number(readline());
  numbers[next] = (numbers[next] || 0) + 1;
  if (Object.keys(numbers).length > 2) {
    print('NO');
    return;
  }
}
var keys = Object.keys(numbers);
if (keys.length === 2 && numbers[keys[0]] === numbers[keys[1]]) {
  print('YES\n' + keys.join(' '));
} else {
  print('NO');
}

})()

var n = parseInt(readline());
var a = [];
for(var i = 0; i < n;i++){
	a[i] = parseInt(readline());
}
// var a = [14,14,14,14,32,32,32,32];

function main(a){
	var len = a.length;
	var map = new Map();
	for(var i = 0; i < len;i++){
		if(map.has(a[i])){
			map.set(a[i],map.get(a[i])+1)
		}else{
			map.set(a[i],1);
		}
	}
	var len1 = map.size;
	if(len1 != 2){
		print('NO');
		// console.log("NO");
	}
	else {
		for(var j = 0; j < len1-1;j++){
			if(map.get(a[j]) === n/2){
				// console.log(map.get(a[j]), map.get(a[j+1]));
				// console.log("YES");
				print('YES');
				var item = map.keys();
				// console.log(item.next().value+' '+item.next().value);
				print(item.next().value+' '+item.next().value)
			}
			else{
				print('NO');
				// console.log("NO");
			}
		}
	}
}
main(a);

var n = parseInt(readline());
var a = [];
for(var i = 0; i < n;i++){
	a[i] = parseInt(readline());
}
// var a = [14,14,14,14,32,32,32,32];
function main(a){
	var len = a.length;
	var map = new Map();
	for(var i = 0; i < len;i++){
		if(map.has(a[i])){
			map.set(a[i],map.get(a[i])+1)
		}else{
			map.set(a[i],1);
		}
	}
	var len1 = map.size;
	if(len1 != 2){
		print('NO');
		// console.log("NO");
	}
	else {
		for(var j = 0; j < len1-1;j++){
			if(map.get(a[j]) === map.get(a[j+1])){
				// console.log(map.get(a[j]), map.get(a[j+1]));
				// console.log("YES");
				print('YES');
				var item = map.keys();
				// console.log(item.next().value+' '+item.next().value);
				print(item.next().value+' '+item.next().value)
			}
			else{
				print('NO');
				// console.log("NO");
			}
		}
	}
}
main(a);

var n = parseInt(readline());
var a = [];
for(var i = 0; i < n;i++){
	a[i] = parseInt(readline());
}
// var a = [1,1,2,2,3,3];
function main(a){
	var len = a.length;
	var map = new Map();
	for(var i = 0; i < len;i++){
		if(map.has(a[i])){
			map.set(a[i],map.get(a[i])+1)
		}else{
			map.set(a[i],1);
		}
	}
	var len1 = map.size;
	if(len1 != 2){
		print('NO');
	}
	else {
		for(var i = 0; i < len1-1;i++){
			if(map.get(a[i]) === map.get(a[i+1])){
				// console.log(map.get(a[i]), map.get(a[i+1]));
				// console.log("Yes");
				// console.log(a[i], a[i+1]);
				print('YES');
				print(a[i],a[i+1]);
			}
			else{
				print('NO');
			}
		}
	}
}
main(a);


The only difference between easy and hard versions is the length of the string.You are given a string $$$s$$$ and a string $$$t$$$, both consisting only of lowercase Latin letters. It is guaranteed that $$$t$$$ can be obtained from $$$s$$$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $$$s$$$ without changing order of remaining characters (in other words, it is guaranteed that $$$t$$$ is a subsequence of $$$s$$$).For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".You want to remove some substring (contiguous subsequence) from $$$s$$$ of maximum possible length such that after removing this substring $$$t$$$ will remain a subsequence of $$$s$$$.If you want to remove the substring $$$s[l;r]$$$ then the string $$$s$$$ will be transformed to $$$s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$$$ (where $$$|s|$$$ is the length of $$$s$$$).Your task is to find the maximum possible length of the substring you can remove so that $$$t$$$ is still a subsequence of $$$s$$$.

The first line of the input contains one string $$$s$$$ consisting of at least $$$1$$$ and at most $$$200$$$ lowercase Latin letters. The second line of the input contains one string $$$t$$$ consisting of at least $$$1$$$ and at most $$$200$$$ lowercase Latin letters. It is guaranteed that $$$t$$$ is a subsequence of $$$s$$$.

Print one integer — the maximum possible length of the substring you can remove so that $$$t$$$ is still a subsequence of $$$s$$$.

if (process.env.ANT)
{
    global.print = this.print ||
        function(){console.log([...arguments].join(' '))} ||
        require('lol-io').print;
    global.debug = function(){console.log('debug:', [...arguments].join(' '))}
    global.write = this.write || require('lol-io').write
    global.readline = this.readline || require('lol-io').readline
    main();
}
else
{
    const rl = require('readline').createInterface({ input: process.stdin,
        output: process.stdout });
    global.print = console.log
    global.debug = ()=>{};
    global.write = (...args) => {
        process.stdout.write(args.join(' '));
    }
    const lines = []
    rl.on('line', line =>{
        lines.push(line);
    });
    rl.on('close', main)
    let rli = 0;
    global.readline = ()=>lines[rli++];
}

///////////// CODE

function main(){
    let D = [];

    let s = readline();
    let t = readline();

    function test(i, m)
    {
        debug('test ', i, m)
        let is = 0, it = 0;
        if (is==i)
            is += m;
        while (it<t.length)
        {
            while (t[it]!=s[is] && is<s.length)
            {
                is++;
                if (is==i)
                    is += m;
            }
            if (is==s.length)
                return false;
            it++;
            is++;
            if (is==i)
                is += m;
        }
        return it==t.length;
    }

    function check(n){
        debug('check '+n)
        for (let i=0; i<s.length-n+1; i++)
        {
            if (test(i, n))
                return true;
        }
    }

    let l = 1, r = s.length-t.length;
    while (l+1<r)
    {
        let m = (l+r)/2|0;
        if (check(m))
            l = m;
        else
            r = m;
    }
    print(check(r) ? r : r-1);

}

'use strict'

const s = readline();
const t = readline();

const a = [], b = [];
let i = -1, j = 0;

while (j < t.length && i < s.length - 1) {
    if (s[++i] === t[j]) a[j++] = i
}

i = s.length; j = t.length - 1;

while (j !== -1 && i !== 0 ) {
    if (s[--i] === t[j]) b[j--] = i
}

let max = Math.max(b[0], s.length - 1 - a[a.length - 1]);

for (let i = 0; i < t.length - 1; i++) {
    if (b[i+1] - a[i] - 1 > max) max = b[i+1] - a[i] - 1;
}

write(max);

if (process.env.ANT)
{
    global.print = this.print ||
        function(){console.log([...arguments].join(' '))} ||
        require('lol-io').print;
    global.debug = function(){console.log('debug:', [...arguments].join(' '))}
    global.write = this.write || require('lol-io').write
    global.readline = this.readline || require('lol-io').readline
    main();
}
else
{
    const rl = require('readline').createInterface({ input: process.stdin,
        output: process.stdout });
    global.print = console.log
    global.debug = ()=>{};
    global.write = (...args) => {
        process.stdout.write(args.join(' '));
    }
    const lines = []
    rl.on('line', line =>{
        lines.push(line);
    });
    rl.on('close', main)
    let rli = 0;
    global.readline = ()=>lines[rli++];
}

///////////// CODE

function main(){
    let D = [];

    let s = readline();
    let t = readline();

    function test(i, m)
    {
        debug('test ', i, m)
        let is = 0, it = 0;
        if (is==i)
            is += m;
        while (it<t.length)
        {
            while (t[it]!=s[is] && is<s.length)
            {
                is++;
                if (is==i)
                    is += m;
            }
            if (is==s.length)
                return false;
            it++;
            is++;
            if (is==i)
                is += m;
        }
        return it==t.length;
    }

    function check(n){
        debug('check '+n)
        for (let i=0; i<s.length-n+1; i++)
        {
            if (test(i, n))
                return true;
        }
    }

    let l = 1, r = s.length-t.length;
    while (l+1<r)
    {
        let m = (l+r)/2;
        if (check(m))
            l = m;
        else
            r = m;
    }
    print(check(r) ? r : r-1);

}

'use strict'

const s = readline();
const t = readline();
const d1 = (s, t) => {
    const a = [], b = [];
    let i = -1, j = 0;
    while (j < t.length && i < s.length - 1) {
        if (s[++i] === t[j]) a[j++] = i
    }
    i = s.length; j = t.length - 1;
    while (j !== -1 && i !== 0 ) {
        if (s[--i] === t[j]) b[j--] = i
    }

    let max = Math.max(b[0], s.length - 1 - a[a.length - 1]);
    for (let i = 0; i < t.length - 1; i++) {
        if (b[i+1] - a[i] - 1 > max) max = b[i+1] - a[i] - 1;
    }
    return max;
}


The protection of a popular program developed by one of IT City companies is organized the following way. After installation it outputs a random five digit number which should be sent in SMS to a particular phone number. In response an SMS activation code arrives.A young hacker Vasya disassembled the program and found the algorithm that transforms the shown number into the activation code. Note: it is clear that Vasya is a law-abiding hacker, and made it for a noble purpose — to show the developer the imperfection of their protection.The found algorithm looks the following way. At first the digits of the number are shuffled in the following order <first digit><third digit><fifth digit><fourth digit><second digit>. For example the shuffle of 12345 should lead to 13542. On the second stage the number is raised to the fifth power. The result of the shuffle and exponentiation of the number 12345 is 455 422 043 125 550 171 232. The answer is the 5 last digits of this result. For the number 12345 the answer should be 71232.Vasya is going to write a keygen program implementing this algorithm. Can you do the same?

The only line of the input contains a positive integer five digit number for which the activation code should be found.

Output exactly 5 digits without spaces between them — the found activation code of the program.

// since javascript only has 53bit number,
// we need to operate it as big number by using string
function multiply(x, strNum)
{
	var carry = 0;
	var num = strNum.split('').map(Number).reverse();
	var result = [];

	for(var i = 0; i < num.length; i++)
	{
		var prod = x * num[i] + carry;
		result.push(prod % 10);
		carry = Math.floor(prod / 10);
	}

	// the possible biggest carry is
	// 99999 * 9 = 899991
	while(carry)
	{
		result.push(carry % 10);
		carry = Math.floor(carry / 10);
	}

	return result.reverse().join('');
}

function pow(x, pow)
{
	var result = '1';
	for(var i = 0; i < pow; i++)
		result = multiply(x, result);

	return result;
}

var input = readline();
var digits = input[0] + input[2] + input[4] + input[3] + input[1];

var result = pow(digits, 5);
print(result.substr(result.length - 5));

function multiply(x, strNum)
{
	var carry = 0;
	var num = strNum.split('').map(Number).reverse();
	var result = [];

	for(var i = 0; i < num.length; i++)
	{
		var prod = x * num[i] + carry;
		result.push(prod % 10);
		carry = Math.floor(prod / 10);
	}

	while(carry)
	{
		result.push(carry % 10);
		carry = Math.floor(carry / 10);
	}

	return result.reverse().join('');
}

function pow(x, pow)
{
	var result = '1';
	for(var i = 0; i < pow; i++)
		result = multiply(x, result);

	return result;
}

var input = readline();
var digits = input[0] + input[2] + input[4] + input[3] + input[1];

var result = pow(digits, 5);
print(result.substr(result.length - 5));


Natasha is going to fly to Mars. She needs to build a rocket, which consists of several stages in some order. Each of the stages is defined by a lowercase Latin letter. This way, the rocket can be described by the string — concatenation of letters, which correspond to the stages.There are $$$n$$$ stages available. The rocket must contain exactly $$$k$$$ of them. Stages in the rocket should be ordered by their weight. So, after the stage with some letter can go only stage with a letter, which is at least two positions after in the alphabet (skipping one letter in between, or even more). For example, after letter 'c' can't go letters 'a', 'b', 'c' and 'd', but can go letters 'e', 'f', ..., 'z'.For the rocket to fly as far as possible, its weight should be minimal. The weight of the rocket is equal to the sum of the weights of its stages. The weight of the stage is the number of its letter in the alphabet. For example, the stage 'a 'weighs one ton,' b 'weighs two tons, and' z' — $$$26$$$ tons.Build the rocket with the minimal weight or determine, that it is impossible to build a rocket at all. Each stage can be used at most once.

The first line of input contains two integers — $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 50$$$) – the number of available stages and the number of stages to use in the rocket. The second line contains string $$$s$$$, which consists of exactly $$$n$$$ lowercase Latin letters. Each letter defines a new stage, which can be used to build the rocket. Each stage can be used at most once.

Print a single integer — the minimal total weight of the rocket or -1, if it is impossible to build the rocket at all.

NoteIn the first example, the following rockets satisfy the condition: "adx" (weight is $$$1+4+24=29$$$); "ady" (weight is $$$1+4+25=30$$$); "bdx" (weight is $$$2+4+24=30$$$); "bdy" (weight is $$$2+4+25=31$$$).Rocket "adx" has the minimal weight, so the answer is $$$29$$$.In the second example, target rocket is "belo". Its weight is $$$2+5+12+15=34$$$.In the third example, $$$n=k=2$$$, so the rocket must have both stages: 'a' and 'b'. This rocket doesn't satisfy the condition, because these letters are adjacent in the alphabet. Answer is -1.

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;
let n, k;

rl.on('line', (d) => {
  if (c === 0) {
    [n, k] = d.split(' ').map(Number);
    c++;
    return;
  }

  const chars = [...d];
  chars.sort();
  const alpha = 'abcdefghijklmnopqrstuvwxyz';

  let ans = -1;
  const str = [chars[0]];

  for (let i = 0; i < n; i++) {
    let prev = str[str.length - 1];
    let distance = Math.abs(alpha.indexOf(prev) - alpha.indexOf(chars[i]));

    if (distance < 2) {
      continue;
    }

    str.push(chars[i]);

    if (str.length === k) {
      break;
    }
  }

  if (str.length >= k) {
    ans = 0;
    for (let i = 0; i < k; i++) {
      ans += alpha.indexOf(str[i]) + 1;
    }
  }

  console.log(ans);
  c++;
});

function main() {
  const is = new Scanner();
  const [n, m] = is.nextArray().map(Number);
  const word = is.nextLine().split('');
  word.forEach((item, index, array) => {
    array[index] = item.charCodeAt() - 'a'.charCodeAt() + 1;
  });
  word.sort((a, b) => a - b);

  function theFor(i, j, accum, number) {
    if (m === number) {
      return accum;
    }
    if (j === n) {
      return -1;
    }

    if (word[j] - word[i] > 1) {
      return theFor(j, j + 1, accum + word[j], number + 1);
    }
    return theFor(i, j + 1, accum, number);
  }

  console.log(theFor(0, 1, word[0], 1));
}

/*
 *  Api Scanner
 */

class Scanner {
  constructor() {
    this.index = 0;
    this.lines = stdinInput.trim().split('\n'); // eslint-disable-line
  }

  nextLine() {
    return this.lines[this.index++]; // eslint-disable-line
  }

  nextInt() {
    return parseInt(this.nextLine(), 10);
  }

  nextDouble() {
    return parseFloat(this.nextLine());
  }

  nextArray() {
    return this.nextLine().split(' ');
  }

  hasNext() {
    return this.index < this.lines.length;
  }
}

let stdinInput = '';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
process.stdin.on('data', (input) => { stdinInput += input; });
process.stdin.on('end', () => { main(); });

function main() {
  const is = new Scanner();
  const [n, m] = is.nextArray().map(Number);
  const word = is.nextLine().split('');
  word.forEach((item, index, array) => {
    array[index] = item.charCodeAt() - 'a'.charCodeAt() + 1;
  });
  word.sort((a, b) => a - b);

  let answer = word[0];
  let pre = 0;
  let number = 1;
  for (let i = 1; i < n; i += 1) {
    if (number === m)
      break;
    if (word[i] - word[pre] >= 2) {
      answer += word[i];
      pre = i;
      number += 1;
    }
  }

  console.log(number !== m ? -1 : answer);
}

/*
 *  Api Scanner
 */

class Scanner {
  constructor() {
    this.index = 0;
    this.lines = stdinInput.trim().split('\n'); // eslint-disable-line
  }

  nextLine() {
    return this.lines[this.index++]; // eslint-disable-line
  }

  nextInt() {
    return parseInt(this.nextLine(), 10);
  }

  nextDouble() {
    return parseFloat(this.nextLine());
  }

  nextArray() {
    return this.nextLine().split(' ');
  }

  hasNext() {
    return this.index < this.lines.length;
  }
}

let stdinInput = '';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
process.stdin.on('data', (input) => { stdinInput += input; });
process.stdin.on('end', () => { main(); });

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;
let n, k;

rl.on('line', (d) => {
  if (c === 0) {
    [n, k] = d.split(' ').map(Number);
    c++;
    return;
  }

  const chars = [...d];
  chars.sort();
  const alpha = 'abcdefghijklmnopqrstuvwxyz';

  let ans = -1;
  const str = [chars[0]];

  for (let i = 0; i < n; i++) {
    let prev = str[str.length - 1];
    let distance = Math.abs(alpha.indexOf(prev) - alpha.indexOf(chars[i]));

    if (distance < 2) {
      continue;
    }

    str.push(chars[i]);

    if (str.length === k) {
      break;
    }
  }

  if (str.length === k) {
    ans = 0;
    for (let i = 0; i < str.length; i++) {
      ans += alpha.indexOf(str[i]) + 1;
    }
  }

  console.log(ans);
  c++;
});

function main() {
  const is = new Scanner();
  const [n, m] = is.nextArray().map(Number);
  const word = is.nextLine().split('');
  word.forEach((item, index, array) => {
    array[index] = item.charCodeAt(0) - 'a'.charCodeAt(0) + 1;
  });
  word.sort((a, b) => a - b);

  let answer = Infinity;

  function calc(i, accum, number) {
    if (number === m) {
      answer = Math.min(answer, accum);
    }
    if (i === n || number > m) {
      return;
    }

    if (Math.abs(word[i] - word[i + 1]) >= 2) {
      calc(i + 1, accum + word[i + 1], number + 1);
    }
    calc(i + 1, accum, number);
  }

  calc(0, word[0], 1);
  console.log(answer === Infinity ? -1 : answer);
}

/*
 *  Api Scanner
 */

class Scanner {
  constructor() {
    this.index = 0;
    this.lines = stdinInput.trim().split('\n'); // eslint-disable-line
  }

  nextLine() {
    return this.lines[this.index++]; // eslint-disable-line
  }

  nextInt() {
    return parseInt(this.nextLine(), 10);
  }

  nextDouble() {
    return parseFloat(this.nextLine());
  }

  nextArray() {
    return this.nextLine().split(' ');
  }

  hasNext() {
    return this.index < this.lines.length;
  }
}

let stdinInput = '';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
process.stdin.on('data', (input) => { stdinInput += input; });
process.stdin.on('end', () => { main(); });

function main() {
  const is = new Scanner();
  const [n, m] = is.nextArray().map(Number);
  const word = is.nextLine().split('');
  word.forEach((item, index, array) => {
    array[index] = item.charCodeAt(0) - 'a'.charCodeAt(0) + 1;
  });
  word.sort((a, b) => a - b);

  let answer = Infinity;

  function calc(i, accum, number) {
    if (number === m) {
      answer = Math.min(answer, accum);
    }
    if (i === n || number > m) {
      return;
    }

    if (Math.abs(word[i] - word[i + 1]) >= 2) {
      calc(i + 1, accum + word[i + 1], number + 1);
    }
    calc(i + 1, accum, number);
  }

  for (let i = 0; i < n - m + 1; i += 1) {
    calc(i, word[i], 1);
  }
  console.log(answer === Infinity ? -1 : answer);
}

/*
 *  Api Scanner
 */

class Scanner {
  constructor() {
    this.index = 0;
    this.lines = stdinInput.trim().split('\n'); // eslint-disable-line
  }

  nextLine() {
    return this.lines[this.index++]; // eslint-disable-line
  }

  nextInt() {
    return parseInt(this.nextLine(), 10);
  }

  nextDouble() {
    return parseFloat(this.nextLine());
  }

  nextArray() {
    return this.nextLine().split(' ');
  }

  hasNext() {
    return this.index < this.lines.length;
  }
}

let stdinInput = '';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
process.stdin.on('data', (input) => { stdinInput += input; });
process.stdin.on('end', () => { main(); });

function main() {
  const is = new Scanner();
  const [n, m] = is.nextArray().map(Number);
  const word = is.nextLine().split('');
  word.forEach((item, index, array) => {
    array[index] = item.charCodeAt(0) - 'a'.charCodeAt(0) + 1;
  });
  word.sort((a, b) => a - b);

  let answer = Infinity;

  function calc(i, accum, number) {
    if (number === m) {
      answer = Math.min(answer, accum);
      return;
    }
    if (i === n) {
      return;
    }

    if (word[i + 1] - word[i] >= 2) {
      calc(i + 1, accum + word[i + 1], number + 1);
    }
    calc(i + 1, accum, number);
  }

  for (let i = 0; i < n - m + 1; i += 1) {
    calc(i, word[i], 1);
  }
  console.log(answer === Infinity ? -1 : answer);
}

/*
 *  Api Scanner
 */

class Scanner {
  constructor() {
    this.index = 0;
    this.lines = stdinInput.trim().split('\n'); // eslint-disable-line
  }

  nextLine() {
    return this.lines[this.index++]; // eslint-disable-line
  }

  nextInt() {
    return parseInt(this.nextLine(), 10);
  }

  nextDouble() {
    return parseFloat(this.nextLine());
  }

  nextArray() {
    return this.nextLine().split(' ');
  }

  hasNext() {
    return this.index < this.lines.length;
  }
}

let stdinInput = '';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
process.stdin.on('data', (input) => { stdinInput += input; });
process.stdin.on('end', () => { main(); });

function main() {
  const is = new Scanner();
  const [n, m] = is.nextArray().map(Number);
  const word = is.nextLine().split('');
  word.forEach((item, index, array) => {
    array[index] = item.charCodeAt() - 'a'.charCodeAt() + 1;
  });
  word.sort((a, b) => a - b);

  function theFor(i, j, accum, number) {
    if (m === number) {
      return accum;
    }
    if (j + 1 === n) {
      return -1;
    }

    if (word[j] - word[i] > 1) {
      return theFor(j, j + 1, accum + word[j], number + 1);
    }
    return theFor(i, j + 1, accum, number);
  }

  console.log(theFor(0, 1, word[0], 1));
}

/*
 *  Api Scanner
 */

class Scanner {
  constructor() {
    this.index = 0;
    this.lines = stdinInput.trim().split('\n'); // eslint-disable-line
  }

  nextLine() {
    return this.lines[this.index++]; // eslint-disable-line
  }

  nextInt() {
    return parseInt(this.nextLine(), 10);
  }

  nextDouble() {
    return parseFloat(this.nextLine());
  }

  nextArray() {
    return this.nextLine().split(' ');
  }

  hasNext() {
    return this.index < this.lines.length;
  }
}

let stdinInput = '';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
process.stdin.on('data', (input) => { stdinInput += input; });
process.stdin.on('end', () => { main(); });

function main() {
  const is = new Scanner();
  const [n, m] = is.nextArray().map(Number);
  const word = is.nextLine().split('');
  word.forEach((item, index, array) => {
    array[index] = item.charCodeAt(0) - 'a'.charCodeAt(0) + 1;
  });
  word.sort((a, b) => a - b);
  console.log(word);
  let answer = Infinity;

  function calc(i, accum, number) {
    if (number === m) {
      answer = Math.min(answer, accum);
      return;
    }
    if (i === n) {
      return;
    }

    if (word[i + 1] - word[i] >= 2) {
      calc(i + 1, accum + word[i + 1], number + 1);
    }
    calc(i + 1, accum, number);
  }

  for (let i = 0; i < n - m + 1; i += 1) {
    calc(i, word[i], 1);
  }
  console.log(answer === Infinity ? -1 : answer);
}

/*
 *  Api Scanner
 */

class Scanner {
  constructor() {
    this.index = 0;
    this.lines = stdinInput.trim().split('\n'); // eslint-disable-line
  }

  nextLine() {
    return this.lines[this.index++]; // eslint-disable-line
  }

  nextInt() {
    return parseInt(this.nextLine(), 10);
  }

  nextDouble() {
    return parseFloat(this.nextLine());
  }

  nextArray() {
    return this.nextLine().split(' ');
  }

  hasNext() {
    return this.index < this.lines.length;
  }
}

let stdinInput = '';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
process.stdin.on('data', (input) => { stdinInput += input; });
process.stdin.on('end', () => { main(); });

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;
let n, k;

rl.on('line', (d) => {
  if (c === 0) {
    [n, k] = d.split(' ').map(Number);
    c++;
    return;
  }

  const chars = [...d];
  chars.sort();
  const alpha = 'abcdefghijklmnopqrstuvwxyz';

  let ans = -1;
  const str = [chars[0]];

  for (let i = 0; i < n; i++) {
    let prev = str[str.length - 1];
    let distance = Math.abs(alpha.indexOf(prev) - alpha.indexOf(chars[i]));

    if (distance <= 2) {
      continue;
    }

    str.push(chars[i]);

    if (str.length === k) {
      break;
    }
  }

  if (str.length === k) {
    ans = 0;
    for (let i = 0; i < str.length; i++) {
      ans += alpha.indexOf(str[i]) + 1;
    }
  }

  console.log(ans);
  c++;
});


Let's denote d(n) as the number of divisors of a positive integer n. You are given three integers a, b and c. Your task is to calculate the following sum:Find the sum modulo 1073741824 (230).

The first line contains three space-separated integers a, b and c (1 ≤ a, b, c ≤ 2000).

Print a single integer — the required sum modulo 1073741824 (230).

function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i], 10);
	};
	return tokens;
}

function numDivisors(x) {
	var count = 0;
	for (var i = 1; i*i <= x; ++i) {
		if (x%i == 0) {
			++count;
			if (i*i != x) {
				++count;
			}
		}
	}
	return count;
}

function main() {
	var data = tokenizeIntegers(readline()),
		a = data[0], b = data[1], c = data[2],
		divisors = {}, count = 0;
	for (var i = 1; i <= a; ++i) {
		for (var j = 1; j <= b; ++j) {
			for (var k = 1; k <= c; ++k) {
				var x = i*j*k;
				if (divisors[x] === undefined) {
					divisors[x] = numDivisors(x);
				}
				count = (count+divisors[x]) % 1073741824;
			}
		}
	}
	print(count);
}

main();

// http://codeforces.com/contest/236/problem/B

var memObj = {};
function d(n) {
    var num = 0;
    if (memObj[n]) {
        num = memObj[n];
    } else {
        var k = Math.floor(Math.sqrt(n));
        for (var i = 1; i <= k; i++) {
            if (n%i === 0) {
                num += 2;
            }

            if (i*i ==n ) num --;
        }
        memObj[n] = num;
    }

    return num;
}

function getSum(a,b,c) {
    var sum = 0;
    for (var i = 1; i <= a; i++) {
        for (var j = 1; j <= b; j++) {
            for (var k =1; k <= c; k++) {
                sum += d(i*j*k);
            }
        }
    }

    return sum;
}

var args = readline().split(' ');
print(getSum(args[0], args[1], args[2])%1073741824);

// http://codeforces.com/contest/236/problem/B

var memObj = {};
function d(n) {
    var num = 0;
    if (memObj[n]) {
        num = memObj[n];
    } else {
        if (n == 1) return 1;
        num = 2;
        var k = Math.floor(Math.sqrt(n));
        for (var i = 2; i <= k; i++) {
            if (n%i === 0) {
                num += 2;
            }

            if (i*i ==n ) num --;
        }
        memObj[n] = num;
    }

    return num;
}

function getSum(a,b,c) {
    var sum = 0;
    for (var i = 1; i <= a; i++) {
        for (var j = 1; j <= b; j++) {
            for (var k =1; k <= c; k++) {
                sum += d(i*j*k);
            }
        }
    }

    return sum;
}

var args = readline().split(' ');
print(getSum(args[0], args[1], args[2])%1073741824);

// http://codeforces.com/contest/236/problem/B

var memObj = {};
function d(n) {
    var num = 0;
    if (memObj[n]) {
        num = memObj[n];
    } else {
        var k = Math.floor(Math.sqrt(n));
        for (var i = 1; i <= k; i++) {
            if (n%i === 0) {
                num += 2;
            }

            if (i*i ==n ) num --;
        }
        memObj[n] = num;
    }

    return num;
}

function getSum(a,b,c) {
    var sum = 0;
    for (var i = 1; i <= a; i++) {
        for (var j = 1; j <= b; j++) {
            for (var k =1; k <= c; k++) {
                sum += d(i*j*k);
            }
        }
    }

    return sum;
}

var args = readline().split(' ');
print(getSum(args[0], args[1], args[2]));

// http://codeforces.com/contest/236/problem/B

var memObj = {};
function d(n) {
    var num = 0;
    if (memObj[n]) {
        num = memObj[n];
    } else {
        var k = Math.floor(Math.sqrt(n));
        for (var i = 1; i <= k; i++) {
            if (k%i === 0) {
                num += 2;
            }

        }
        if (k*k == n) {
            num -= 1;
        }

        memObj[n] = num;
    }

    return num;
}

function getSum(a,b,c) {
    var sum = 0;
    for (var i = 1; i <= a; i++) {
        for (var j = 1; j <= b; j++) {
            for (var k =1; k <= c; k++) {
                sum += d(i*j*k);
            }
        }
    }

    return sum;
}

var args = readline().split(' ');
print(getSum(args[0], args[1], args[2]));